你已经得到了很好的答案。在简洁的追逐,我提出我的16号线的解决方案,基于Dimitres answer:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<choices>
<xsl:for-each-group select="option" group-by="title">
<choice>
<xsl:sequence select="title"/>
<xsl:for-each-group select="current-group()/desc" group-by=".">
<xsl:sequence select="."/>
</xsl:for-each-group>
</choice>
</xsl:for-each-group>
</choices>
</xsl:template>
</xsl:stylesheet>
注意里面for-each-group
当前上下文节点是当前组中的第一项,而current-group()
返回的所有项目的列表当前组。我利用title
元素对于输入和输出是相同的事实,并复制每个组的第一个标题。
并且为了完整性,使用Muenchian分组的(20线)的XSLT 1.0溶液:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="title" match="option/title" use="."/>
<xsl:key name="desc" match="option/desc" use="."/>
<xsl:template match="/*">
<choices>
<xsl:for-each select="option/title[count(.|key('title',.)[1]) = 1]">
<choice>
<xsl:copy-of select="."/>
<xsl:for-each select="key('title',.)/../desc
[count(.|key('desc', .)[../title=current()][1]) = 1]">
<xsl:copy-of select="."/>
</xsl:for-each>
</choice>
</xsl:for-each>
</choices>
</xsl:template>
</xsl:stylesheet>
很好的问题(1)。查看我的答案,了解最低(19行)XSLT 2.0解决方案。 :) – 2010-04-28 13:12:54