XSLT 1.0-创建唯一的,有序列表 [英] XSLT 1.0 - Create a Unique, Ordered List
问题描述
我正在尝试创建一个转换,在该转换中生成一个有序的唯一列表(连字符将两个值分开.
I'm trying to create a transform where I generate an ordered unique list (where a hyphen separates two values.
我有资料来源
<?xml version="1.0"?>
<results>
<result>
<Name>Blue</Name>
<Author>Hat</Author>
<TrackNum>5</TrackNum>
</result>
<result>
<Name>Red</Name>
<Author>Car</Author>
<TrackNum>2</TrackNum>
</result>
<result>
<Name>Blue</Name>
<Author>Hat</Author>
<TrackNum>345</TrackNum>
</result>
</results>
并希望输出(按名称"排序)
And want the output (ordered by 'Name')
:: Blue - Hat
:: Red - Car
XLST-当前不返回任何内容(我知道它也缺少第二个带连字符/连接的值)
XLST - This currently returns nothing (I know it's missing the second, hyphenated/concatenated value too)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:key name="Name" match="/results/result/Name/text()" use="." />
<xsl:template match="/">
<xsl:for-each select="/results/results/Name/text()[generate-id()
= generate-id(key('Name',.)[1])]">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
推荐答案
您处在正确的轨道上. Xsl:for-each选择的XPath中有一个type-o,/results/results
应该为/results/result
.我对键的匹配和使用做了些微调整.
You were on the right track. You had a type-o in the XPath for your xsl:for-each select: /results/results
should be /results/result
. I made a slight adjustment to the key for what it matched against and used.
以下内容生成名称和作者组合的唯一列表,按Name
,然后按Author
排序.
The following produces a unique list of Name and Author combinations, sorted by Name
and then Author
.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:key name="Name-Author" match="results/result" use="concat(Name, ' - ', Author)" />
<xsl:template match="/">
<xsl:for-each select="/results/result[generate-id()
= generate-id(key('Name-Author',concat(Name, ' - ', Author))[1])]">
<xsl:sort select="Name"/>
<xsl:sort select="Author"/>
<li>
<xsl:value-of select="concat(Name, ' - ', Author)"/>
</li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT 1.0-创建唯一的,有序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!