XSL Grouped Count

x0127960

New Member
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:\[code\]<A> <B> <C> <D>APPLE</D> </C> </B> <B> <C> <D>BANANA</D> </C> </B> <B> <C> <D>APPLE</D> </C> </B></A>\[/code\]Based on the code in the previous answer I have:\[code\]<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>\[/code\]But this returns:\[code\]APPLE - 2BANANA - 1APPLE - 2\[/code\]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.
 
Back
Top