What is the best way to implement sorting using XSLT?
Input: <cross-refs>5,8,4,3,9</cross-refs>
Excepted output: <cross-refs>3,4,5,8,9</cross-refs>
Thanks in advance
Try:
<xsl:template match="cross-refs">
<xsl:copy>
<xsl:value-of separator=",">
<xsl:perform-sort select="tokenize(., ',')">
<xsl:sort select="." data-type="number"/>
</xsl:perform-sort>
</xsl:value-of>
</xsl:copy>
</xsl:template>
Note that this assumes you want to sort in numerical, not alphabetical, order.
In XSLT 3.0 you could reduce this to:
<xsl:template match="cross-refs">
<xsl:copy>
<xsl:value-of select="sort(tokenize(., ',')!xs:integer(.))" separator=","/>
</xsl:copy>
</xsl:template>