我正在尝试获取元素祖先,自我和后代的属性值。然后我想显示唯一值。
这就是我想要的。
<xsl:key name="remove" match="value" use="sortedvalues" />
<xsl:for-each select="ancestor-or-self::node()/@value[generate-id() = generate-id(key('remove',sortedvalues)[1])] | descendant-or-self::node()/@value[generate-id() = generate-id(key('remove',sortedvalues)[1])]">
<xsl:for-each select="key('remove',sortedvalues)">
<xsl:sort select="sortedvalues"></xsl:sort>
</xsl:for-each>
</xsl:for-each>
这是实际的XML
<Root>
<something value = “asd” </something>
<something value = “dsa” </something>
<Product>
<something value = "asd"></something>
<something value = "dsa"></something>
<something value = "asd"></something>
<anothernode>
<something value = "qwe"></something>
</anothernode>
<anothernode2>
<something value = "ewq"></something>
</anothernode2>
<something value = "ewq"></something>
<Product>
<Product>
<something value = "asd"></something>
<something value = "dsa"></something>
<something value = "asd"></something>
<anothernode>
<something value = "qwe"></something>
</anothernode>
<anothernode2>
<something value = "ewq"></something>
</anothernode2>
<something value = "ewq"></something>
<Product>
</Root>
我基本上想要表演
<Product>
<values>asd <values>
<values>dsa<values>
<values>qwe<values>
<values>ewq<values>
<Product>
<Product>
<values>asd <values>
<values>dsa<values>
<values>qwe<values>
<values>ewq<values>
<Product>
所以我不能使用//来展示每个产品,所以我一直在产品上使用祖先和后代
答案 0 :(得分:0)
如果您想按产品获取唯一的value
属性,则必须引用密钥的产品部分
<xsl:key name="values" match="@value" use="concat(generate-id(ancestor::Product), '|', .)" />
然后,对于给定的产品,您将得到如此不同的值(其中$id
是一个包含产品的generate-id()值的变量)
<xsl:for-each select=".//@value[generate-id() = generate-id(key('values', concat($id, '|', .))[1])]">
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="values" match="@value" use="concat(generate-id(ancestor::Product), '|', .)" />
<xsl:template match="Product">
<xsl:variable name="id" select="generate-id()" />
<xsl:copy>
<xsl:for-each select=".//@value[generate-id() = generate-id(key('values', concat($id, '|', .))[1])]">
<values><xsl:value-of select="." /></values>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
注意,如果您可以升级到XSLT 2.0,那么您可以使用xsl:for-each-group
,如此
<xsl:for-each-group select=".//@value" group-by=".">
或者您可以使用distinct-values()
,就像这样
<xsl:for-each select="distinct-values(.//@value)">