好的,所以我知道XSLT,为了练习,我从gov uk网站下载了一个数据集。我正在研究一组关于英国布莱顿和霍夫食品行业卫生等级的数据。
我的XML数据文件看起来像.,但是要长得多
<?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提供的代码是:
<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和显示的结果。我也尝试过这个代码:
<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中不同的值函数来实现这个目标吗?
发布于 2018-01-15 01:50:19
对于XSLT1.0,请考虑将键调整为特定值RatingValue,然后遍历树,而不是<xsl:for-each>。然后有条件地指定只提取那些值,生成的键是每个不同值的第一个分组:
<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>
<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在输出中不重复,值被排序。
<output>
<rating>3</rating>
<rating>4</rating>
<rating>5</rating>
</output>对于HTML输出,只需将<output>和<rating>标记替换为HTML标记:
<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>输出
<html>
<body>
<ul>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</body>
</html>https://stackoverflow.com/questions/48253302
复制相似问题