How to count distinct values in a node in XSLT?
Example: I want to count the number of existing countries in Country nodes, in this case, it would be 3.
<Artists_by_Countries>
<Artist_by_Country>
<Location_ID>62</Location_ID>
<Artist_ID>212</Artist_ID>
<Country>Argentina</Country>
</Artist_by_Country>
<Artist_by_Country>
<Location_ID>4</Location_ID>
<Artist_ID>108</Artist_ID>
<Country>Australia</Country>
</Artist_by_Country>
<Artist_by_Country>
<Location_ID>4</Location_ID>
<Artist_ID>111</Artist_ID>
<Country>Australia</Country>
</Artist_by_Country>
<Artist_by_Country>
<Location_ID>12</Location_ID>
<Artist_ID>78</Artist_ID>
<Country>Germany</Country>
</Artist_by_Country>
</Artists_by_Countries>
If you have a large document, you probably want to use the "Muenchian Method", which is usually used for grouping, to identify the distinct nodes. Declare a key that indexes the things you want to count by the values that are distinct:
Then you can get the
<Artist_by_Country>
elements that have distinct countries using:and you can count them by wrapping that in a call to the
count()
function.Of course in XSLT 2.0, it's as simple as
In XSLT 1.0 this isn't obvious, but the following should give you an idea of the requirement:
The more elements in your XML the longer this takes, as it checks every single preceding sibling of every single element.
Try something like this:
"Give me the count of all Country nodes without a following Country with matching text"
The interesting bit of that expression, IMO, is the following axis.
You could probably also remove the first
/text()
, and replace the second with.
If you have control of the xml generation on the first occurence of a country you could add an attribute to the country node such as distinct='true' flag the country as "used" and not subsequently add the distinct attribute if you come across that country again.
You could then do