首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用XSL 1列出XML中的所有唯一值,以及如何在XSL 2中实现相同的值?

如何使用XSL 1列出XML中的所有唯一值,以及如何在XSL 2中实现相同的值?
EN

Stack Overflow用户
提问于 2018-01-14 19:22:31
回答 1查看 345关注 0票数 1

好的,所以我知道XSLT,为了练习,我从gov uk网站下载了一个数据集。我正在研究一组关于英国布莱顿和霍夫食品行业卫生等级的数据。

我的XML数据文件看起来像.,但是要长得多

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
        <?xml-stylesheet type = "text/xsl" href = "test5.xsl"?> 
<FHRSEstablishment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <Header>
                <ExtractDate>2018-01-13</ExtractDate>
                <ItemCount>3074</ItemCount>
                <ReturnCode>Success</ReturnCode>
            </Header>
                <EstablishmentCollection>
                    <EstablishmentDetail>
                        <FHRSID>736377</FHRSID>
                        <LocalAuthorityBusinessID>2014/00302/COMM</LocalAuthorityBusinessID>
                        <BusinessName>16</BusinessName>
                        <BusinessType>Restaurant/Cafe/Canteen</BusinessType>
                        <BusinessTypeID>1</BusinessTypeID>
                        <AddressLine1>16 Sydney Street</AddressLine1>
                        <AddressLine2>Brighton</AddressLine2>
                        <PostCode>BN1 4EN</PostCode>
                        <RatingValue>5</RatingValue>
                        <RatingKey>fhrs_5_en-GB</RatingKey>
                        <RatingDate>2017-01-12</RatingDate>
                        <LocalAuthorityCode>875</LocalAuthorityCode>
                        <LocalAuthorityName>Brighton and Hove</LocalAuthorityName>
                        <LocalAuthorityWebSite>http://www.brighton-hove.gov.uk/foodsafety</LocalAuthorityWebSite>
                        <LocalAuthorityEmailAddress>ehl.food@brighton-hove.gov.uk</LocalAuthorityEmailAddress>
                            <Scores>
                                <Hygiene>0</Hygiene>
                                <Structural>0</Structural>
                                <ConfidenceInManagement>0</ConfidenceInManagement>
                            </Scores>
                        <SchemeType>FHRS</SchemeType>
                        <NewRatingPending>False</NewRatingPending>
                            <Geocode>
                                <Longitude>-0.13802900000000</Longitude>
                                <Latitude>50.82747700000000</Latitude>
                            </Geocode>
                    </EstablishmentDetail>
                    <EstablishmentDetail>
                        <FHRSID>722916</FHRSID>
                        <LocalAuthorityBusinessID>2014/00536/COMM</LocalAuthorityBusinessID>
                        <BusinessName>18 Grocery</BusinessName>
                        <BusinessType>Retailers - other</BusinessType>
                        <BusinessTypeID>4613</BusinessTypeID>
                        <AddressLine1>Unit 18</AddressLine1>
                        <AddressLine2>Open Market</AddressLine2>
                        <AddressLine3>Marshalls Row</AddressLine3>
                        <AddressLine4>Brighton</AddressLine4>
                        <PostCode>BN1 4JU</PostCode>
                        <RatingValue>4</RatingValue>
                        <RatingKey>fhrs_4_en-GB</RatingKey>
                        <RatingDate>2014-12-01</RatingDate>
                        <LocalAuthorityCode>875</LocalAuthorityCode>
                        <LocalAuthorityName>Brighton and Hove</LocalAuthorityName>
                        <LocalAuthorityWebSite>http://www.brighton-hove.gov.uk/foodsafety</LocalAuthorityWebSite>
                        <LocalAuthorityEmailAddress>ehl.food@brighton-hove.gov.uk</LocalAuthorityEmailAddress>
                            <Scores>
                                <Hygiene>0</Hygiene>
                                <Structural>5</Structural>
                                <ConfidenceInManagement>10</ConfidenceInManagement>
                            </Scores>
                        <SchemeType>FHRS</SchemeType>
                        <NewRatingPending>False</NewRatingPending>
                            <Geocode>
                                <Longitude>-0.13602300000000</Longitude>
                                <Latitude>50.83150100000000</Latitude>
                            </Geocode>
                    </EstablishmentDetail>
                </EstablishmentCollection>
 </FHRSEstablishment>

我在这里感兴趣的是列出RatingValue的所有唯一/不同的值,以查找所有可能的选项。

我一直在研究和尝试各种不同的方法,但似乎没有什么对我有用。所有其他示例都基于更简单的XML数据,我很难过滤我的文件。

我为XSL1.0提供的代码是:

代码语言:javascript
复制
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>  

 <xsl:key name="stars" match="/FHRSEstablishment/EstablishmentCollection/*" use="name()" /> 

 <xsl:template match="/">
    <xsl:for-each select="/FHRSEstablishment/EstablishmentCollection/*
    [count(. | key('stars', name())[1]) = 1]">
        <xsl:sort select="name()" />
        <xsl:value-of select="RatingValue" />
    </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

但是我得到的输出只是一个数字5

我知道只有5可定位的评级从1-5,但我想找到这个使用XSL和显示的结果。我也尝试过这个代码:

代码语言:javascript
复制
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>  

 <xsl:key name="stars" match="/FHRSEstablishment/EstablishmentCollection/*" use="name()" /> 

 <xsl:template match="/">
    <xsl:for-each select="/FHRSEstablishment/EstablishmentCollection/*
    [count(. | key('stars', name())[1]) = 1]">
        <xsl:sort select="name()" />
        <xsl:value-of select="name()" />
    </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

但是我在这里得到的输出是:EstablishmentDetail

有人能帮我解释一下我如何在XSL 1.0中使用“key”和xsl 2中不同的值函数来实现这个目标吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-15 01:50:19

对于XSLT1.0,请考虑将键调整为特定值RatingValue,然后遍历树,而不是<xsl:for-each>。然后有条件地指定只提取那些值,生成的键是每个不同值的第一个分组:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              exclude-result-prefixes="xsi">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>  

 <xsl:key name="stars" match="EstablishmentDetail" use="RatingValue" /> 

 <xsl:template match="/FHRSEstablishment">
   <output>
    <xsl:apply-templates select="EstablishmentCollection"/>
   </output>
 </xsl:template>

 <xsl:template match="EstablishmentCollection">
    <xsl:apply-templates select="EstablishmentDetail[generate-id() = 
                                 generate-id(key('stars', RatingValue))]">
          <xsl:sort select="RatingValue" />
    </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="EstablishmentDetail">
   <rating>
      <xsl:value-of select="RatingValue"/>
   </rating>
 </xsl:template>

</xsl:stylesheet>

演示

对于XSLT2.0,请考虑缩小RatingValue的父级,然后运行<xsl:for-each-group>

代码语言:javascript
复制
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              exclude-result-prefixes="xsi">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>  

 <xsl:template match="/FHRSEstablishment">
   <output>
    <xsl:apply-templates select="EstablishmentCollection"/>
   </output>
 </xsl:template>

 <xsl:template match="EstablishmentCollection">
    <xsl:for-each-group select="EstablishmentDetail" group-by="RatingValue"> 
      <xsl:sort select="RatingValue"/>
      <rating>
        <xsl:value-of select="RatingValue"/>
      </rating>
    </xsl:for-each-group>
 </xsl:template>

</xsl:stylesheet>

演示

对于演示,我将发布的XML扩展到包含多个RatingValues (复制元素节),其值为5、4、4、3。注意到4在输出中不重复,值被排序。

代码语言:javascript
复制
<output>
  <rating>3</rating>
  <rating>4</rating>
  <rating>5</rating>
</output>

对于HTML输出,只需将<output><rating>标记替换为HTML标记:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              exclude-result-prefixes="xsi">
 <xsl:output omit-xml-declaration="yes" indent="yes" method="html"/>  

 <xsl:key name="stars" match="EstablishmentDetail" use="RatingValue" /> 

 <xsl:template match="/FHRSEstablishment">
   <html>
     <body>
       <xsl:apply-templates select="EstablishmentCollection"/>
     </body>
   </html>
 </xsl:template>

 <xsl:template match="EstablishmentCollection">
    <ul>
       <xsl:apply-templates select="EstablishmentDetail[generate-id() = generate-id(key('stars', RatingValue))]">
          <xsl:sort select="RatingValue" />
       </xsl:apply-templates>
    </ul>
 </xsl:template>

 <xsl:template match="EstablishmentDetail">
   <li>
      <xsl:value-of select="RatingValue"/>
   </li>
 </xsl:template>

</xsl:stylesheet>

输出

代码语言:javascript
复制
<html>
  <body>
    <ul>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>
  </body>
</html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48253302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档