Without testing it myself, here's what I would change:
<xsl:for-each select="key('country', COUNTRY)">
<xsl:value-of select="count(//CD)"/>
</xsl:for-each>
to
<xsl:value-of select="count(key('country', COUNTRY))" />
Explanation:
- You have the outer loop doing a proper Muenchian-grouped "for each country"; or more precisely, "for each CD that is the first CD for its respective country". (Good job applying the Muenchian technique BTW, if that's your first time.)
- Inside that loop, you're asking XPath to count the number of nodes in the set returned by the key for the COUNTRY of the current CD.
Bonus: I like to name keys following Jeni Tennison's example, 'Xs-by-Y', where X is the type of thing returned by the key, and Y is the type of value passed to the key (i.e. what is referenced by the use=
attribute). This makes it much easier for me to think straight when defining the key and when using it, so that I don't get confused between what the key()
function takes and what it returns. So in your case, naming the key 'CDs-by-COUNTRY'
would make the code more readable and help prevent mistakes.
You might choose a different key naming convention, but it helps to have something that makes the identity of the key vs. the value explicit. Especially if you're not doing type checking.