1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xpath-sort.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.3 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
sort() Sorts a sequence of items using an optional collation and key function, returning items in ascending order. 2026-04-18T00:00:00Z 3.0 XSLT 3.0 higher-order function sort(sequence, collation?, key-function?)
xslt
reference
xpath
xslt3

Description

sort() sorts a sequence of items and returns them in ascending order. An optional collation URI controls string comparison, and an optional key function extracts the sort key from each item. This is the functional alternative to xsl:sort and can be used directly within XPath expressions.

Parameters

Parameter Type Required Description
sequence item()* Yes The sequence to sort.
collation xs:string? No URI of the collation to use for string comparison. Defaults to the default collation.
key-function function(item()) as xs:anyAtomicType* No Function that extracts the sort key from each item. Defaults to identity.

Return value

item()* — the items in ascending order according to the sort key and collation.

Examples

Sorting strings alphabetically

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <result>
      <xsl:variable name="fruits" select="('banana', 'apple', 'cherry', 'date')"/>
      <xsl:for-each select="sort($fruits)">
        <fruit><xsl:value-of select="."/></fruit>
      </xsl:for-each>
    </result>
  </xsl:template>
</xsl:stylesheet>

Output:

<result>
  <fruit>apple</fruit>
  <fruit>banana</fruit>
  <fruit>cherry</fruit>
  <fruit>date</fruit>
</result>

Sorting XML nodes by a numeric attribute

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
  <employee name="Alice" salary="75000"/>
  <employee name="Bob"   salary="50000"/>
  <employee name="Carol" salary="90000"/>
</employees>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/employees">
    <sorted>
      <xsl:copy-of select="sort(employee, (), function($e) { xs:integer($e/@salary) })"/>
    </sorted>
  </xsl:template>
</xsl:stylesheet>

Output:

<sorted>
  <employee name="Bob"   salary="50000"/>
  <employee name="Alice" salary="75000"/>
  <employee name="Carol" salary="90000"/>
</sorted>

Notes

  • Sorting is stable: items with equal keys preserve their original relative order.
  • Pass () as the collation argument to use the default collation while still providing a key function.
  • For descending sort in XPath expressions, use reverse(sort(...)).
  • sort() is also available as array:sort() for sorting array members.
  • In XSLT templates, xsl:sort inside xsl:for-each or xsl:apply-templates remains an alternative.

See also