I am trying to extract unique values from an XML and how many times they occur.
I have been following the answer given in Xslt distinct select / Group by but my schema is a little different.
My XML looks something similar to:
<A>
<B>
<C>
<D>APPLE</D>
</C>
</B>
<B>
<C>
<D>BANANA</D>
</C>
</B>
<B>
<C>
<D>APPLE</D>
</C>
</B>
</A>
Based on the code in the previous answer I have:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:key
name="C-by-DValue"
match="B/C/D"
use="text()"
/>
<xsl:template match="A">
<xsl:for-each select="
B/C/D[
count(
. | key('C-by-DValue', B/C/D/text())[1]
) = 1
]
">
<xsl:value-of select="text()"/>
<xsl:value-of select="' - '"/>
<!-- simple: the item count is the node count of the key -->
<xsl:value-of select="
count(
key('C-by-DValue', text())
)
"/>
<xsl:value-of select="' '"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But this returns:
APPLE - 2
BANANA - 1
APPLE - 2
So the for-each-select isn't only matching the first instance of each text() value. Could someone point me in the right direction please.