In XSLT 1.0, your best bet is to use a two-step approach.
- tokenize the input from comma-delimited into separate elements
- group on the separate elements
Step #1 tokenizes the input:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="/root">
<countries>
<xsl:apply-templates select="entry" />
</countries>
</xsl:template>
<xsl:template match="entry">
<xsl:call-template name="tokenize">
<xsl:with-param name="input" select="countries" />
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="input" />
<xsl:variable name="list" select="concat($input, ',')" />
<xsl:variable name="head" select="substring-before($list, ',') " />
<xsl:variable name="tail" select="substring-after($list, ',') " />
<xsl:if test="normalize-space($head) != ''">
<country>
<xsl:value-of select="normalize-space($head)" />
</country>
<xsl:call-template name="tokenize">
<xsl:with-param name="input" select="$tail" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
produces:
<countries>
<country>USA</country>
<country>Australia</country>
<country>Canada</country>
<country>USA</country>
<country>Australia</country>
<country>Australia</country>
<country>Belgium</country>
<country>Croatia</country>
</countries>
Step #2 applies Muenchian grouping to the intermediary result:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text" />
<xsl:key name="kCountry" match="country" use="." />
<xsl:template match="/countries">
<xsl:apply-templates select="country">
<xsl:sort select="count(key('kCountry', .))" data-type="number" order="descending" />
<xsl:sort select="." data-type="text" order="ascending" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="country">
<xsl:if test="generate-id() = generate-id(key('kCountry', .)[1])">
<xsl:value-of select="." />
<xsl:text>	</xsl:text>
<xsl:value-of select="count(key('kCountry', .))" />
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
produces the wanted result (formatting is left as an exercise for the reader):
Australia 3
USA 2
Belgium 1
Canada 1
Croatia 1
The process can be done in a single transformation, with the help of the node-set()
extension function. However, you would lose the ability to use an XSL key, which might result in slower performance for large inputs. YMMV.
The necessary modification of step #1 would be (using the MSXSL extensions, other vendors differ in the namespace declaration, which reduces portability of this approach):
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
>
<xsl:template match="/root">
<!-- store the list of <country>s as a result-tree-fragment -->
<xsl:variable name="countries">
<xsl:apply-templates select="entry" />
</xsl:variable>
<!-- convert the result-tree-fragment to a usable node-set -->
<xsl:variable name="country" select="msxsl:node-set($countries)/country" />
<!-- iteration, sorting and grouping in one step -->
<xsl:for-each select="$country">
<xsl:sort select="count($country[. = current()])" data-type="number" order="descending" />
<xsl:sort select="." data-type="text" order="ascending" />
<xsl:if test="generate-id() = generate-id($country[. = current()][1])">
<xsl:value-of select="." />
<xsl:text>	</xsl:text>
<xsl:value-of select="count($country[. = current()])" />
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- ... the remainder of the stylesheet #1 is unchanged ... -->
</xsl:stylesheet>
With this approach, a separate step #2 becomes unnecessary. The result is the same as above. For small inputs, the difference in performance will not be noticeable.