XSL unique values per node
Posted
by Nathan
on Stack Overflow
See other posts from Stack Overflow
or by Nathan
Published on 2010-04-29T23:33:14Z
Indexed on
2010/04/29
23:37 UTC
Read the original article
Hit count: 410
ok i have this xml
<roots>
<root>
<name>first</name>
<item type='test'><something>A</something></item>
<item type='test'><something>B</something></item>
<item type='test'><something>C</something></item>
<item type='test'><something>A</something></item>
<item type='other'><something>A</something></item>
<item type='test'><something>B</something></item>
<item type='other'><something>D</something></item>
</root>
<root>
<name>second</name>
<item type='test'><something>E</something></item>
<item type='test'><something>B</something></item>
<item type='test'><something>F</something></item>
<item type='test'><something>A</something></item>
<item type='other'><something>A</something></item>
<item type='test'><something>B</something></item>
<item type='other'><something>D</something></item>
</root>
</roots>
now i need to get the unique values of each root node so far i have
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="text"/>
<xsl:key name="item-by-value" match="something" use="."/>
<xsl:key name="rootkey" match="root" use="name"/>
<xsl:template match="/">
<xsl:for-each select="key('rootkey','second')">
<xsl:for-each select="item/something">
<xsl:if test="generate-id() = generate-id(key('item-by-value', normalize-space(.)))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
if i use "First" as the key to get only the first root i get a good result ABCD
how ever if i use "second" i only get EF but i need the result to be ABDFE
© Stack Overflow or respective owner