如果你是热衷于了解XSLT的复杂性,你可能有兴趣知道的一种叫做Muenchian Grouping的技术可以用来解决你的问题。要获得不同的行,可以通过attribute1将它们有效地分组,然后为每个组选择组中的第一个元素。 (或丢弃不是第一个的元素)。 Muenchian分组是在XSLT 1.0中实现这一点的最有效方式。
在这种情况下,你开始通过定义键表示你的团队,而你的情况是排元素,通过ATTRIBUTE1
<xsl:key name="row" match="row" use="@attribute1" />
分组然后,如果您想选择不同排元素,你会选择行最先发生在键对他们给予ATTRIBUTE1元素
<xsl:apply-templates select="row[generate-id() = generate-id(key('row', @attribute1)[1])]" />
或者,您可以有一个模板来忽略行重复的元素(即,È哪些不是第一组中)
<xsl:template match="row[generate-id() != generate-id(key('row', @attribute1)[1])]" />
尝试此XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="row" match="row" use="@attribute1" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="row[generate-id() != generate-id(key('row', @attribute1)[1])]" />
</xsl:stylesheet>
当施加到下面的XML
<rows>
<row attribute1="1" attribute2="something" attribute3="somevalue" />
<row attribute1="1" attribute2="something" attribute3="somevalue" />
<row attribute1="2" attribute2="anotherthing" attribute3="somevalue" />
</rows>
以下是输出
<rows>
<row attribute1="1" attribute2="something" attribute3="somevalue"></row>
<row attribute1="2" attribute2="anotherthing" attribute3="somevalue"></row>
</rows>
XSLT很容易就可以倾向于重新命名属性或将其排除,如ljdelight的回答中所述。此外,如果你想在您的测试第二个属性,你可以扩展你的关键,像这样:
<xsl:key name="row" match="row" use="concat(@attribute1, '|', @attribute3)" />
,并忽略重复,模板会看这本
<xsl:template match="row
[generate-id() != generate-id(key('row', concat(@attribute1, '|', @attribute3))[1])]" />
唯一这里需要注意的是使用管道字符|作为分隔符。如果需要,这可以是任何其他字符,只要它不出现在属性值中即可。
非常感谢,它的工作。如果我想过滤多个属性,我应该将它们添加到'之前的兄弟姐妹'表达式中? – sblandin 2013-03-05 10:49:54
对。如果您想按不同的属性1和属性3对进行过滤,那么应该是 row [prior-sibling :: row/\ @ attribute1 = \ @ attribute1 and preceding-sibling :: row/\ @ attribute3 = \ @ attribute3] – ljdelight 2013-03-05 11:54:21
(忽略反斜杠...所以抱怨说,我试图通知太多的用户) – ljdelight 2013-03-05 11:55:01