0

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.