这是一个常见问题。见http://www.jenitennison.com/xslt/grouping/muenchian.html
该XSLT代码:
<?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" indent="yes" encoding="UTF-8"/>
<xsl:key name="criteria" match="/Nodes/Node" use="@att"/>
<xsl:template match="Nodes">
<xsl:copy>
<xsl:apply-templates select="Node[generate-id() = generate-id(key('criteria', @att))]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Node">
<xsl:copy-of select="."/> <!-- Or other actions -->
</xsl:template>
</xsl:stylesheet>
将提供所需的(如果我理解正确的)输出:
<?xml version="1.0" encoding="UTF-8"?>
<Nodes>
<Node att="1">A</Node>
<Node att="2">C</Node>
<Node att="3">E</Node>
</Nodes>
它也将与像输入工作,例如:
<Nodes>
<Node att="someRandomString">A</Node>
<Node att="1aeawe">B</Node>
<Node att="someRandomString">C</Node>
<Node att="sfdf">D</Node>
<Node att="">E</Node>
<Node att="sfdf">F</Node>
</Nodes>
输出结果为:
<?xml version="1.0" encoding="UTF-8"?>
<Nodes>
<Node att="someRandomString">A</Node>
<Node att="1aeawe">B</Node>
<Node att="sfdf">D</Node>
<Node att="">E</Node>
</Nodes>
这里有一个非常类似的问题,有很多答案:http://stackoverflow.com/questions/399204/xslt-distinct-elements-and-分组 – 2010-12-10 23:03:27
“ACE”输出将用于每个独立值的第一个(文档顺序)。请做澄清。 – 2010-12-10 23:05:30
好问题,+1。查看我的答案可能是最简单和最短的解决方案 - XPath单行程式。 :) – 2010-12-11 00:10:57