Skip to content

XSL select distinct elements

After upgrading to XSLT 2.0 and Saxon 8, I discovered that the saxon:distinct function was removed.

The variable $languages looks like:

<language>English</language>
<language>Chinese</language>
<language>English</language>
<language>French</language>
<language>French</language>

It contains 5 language elements, but we only want to process the 3 distinct languages.

In the past, I used the saxon:distinct function, specific to Saxon. Other XSL processors have their own functions.

<xsl:for-each select="saxon:distinct($languages/language)">
language found: <xsl:value-of select="."/>
</xsl:for-each>

For XSLT 2.0, the above can be replaced with:

<xsl:for-each select="distinct-values($languages/lang)">
<xsl:value-of select="."/>
</xsl:for-each>