用于所有节点的 XSLT MUENCHIAN 方法
XSLT MUENCHIAN METHOD for all the nodes
我正在尝试获取元素祖先、自身和后代的属性值。然后我想显示唯一值。
这就是我正在尝试的。
<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>
所以我不能使用 // 来显示每个单独的产品,所以我一直在产品上使用祖先和后代
如果要按产品获取唯一 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)">
我正在尝试获取元素祖先、自身和后代的属性值。然后我想显示唯一值。
这就是我正在尝试的。
<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>
所以我不能使用 // 来显示每个单独的产品,所以我一直在产品上使用祖先和后代
如果要按产品获取唯一 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)">