What Data Types Does XSLT Sort Support?
The data-type attribute in the XSLT
<xsl:sort> element controls how items are compared
during node ordering, distinguishing between alphabetic collation and
numerical evaluation. The standard W3C XSLT 1.0 and 2.0/3.0
specifications natively define two core enumerated
values—text and number—while also allowing
QName extension types or explicit XML Schema data types depending on the
processor version.
Standard Data Types in XSLT 1.0
In XSLT 1.0, the data-type attribute accepts two primary
standard values:
text(Default): Instructs the XSLT processor to perform lexicographical (alphabetic) sorting based on Unicode code points or local collation rules. Undertextsorting, the string"10"precedes"2"because the character'1'comes before'2'.number: Instructs the processor to evaluate the sort keys as floating-point numbers. Non-numeric values or empty nodes are evaluated asNaN(Not-a-Number), which typically sort before all valid numerical values in standard implementations.
XSLT 1.0 also allows a prefixed QName (Qualified Name)
to support implementation-defined, vendor-specific sorting algorithms,
though portable stylesheets generally stick to text and
number.
Enhancements in XSLT 2.0 and 3.0
XSLT 2.0 and XSLT 3.0 expand on sorting capabilities through integration with W3C XML Schema data types:
- Standard Built-in Types: In addition to
textandnumber, processors supporting Schema-Aware XSLT allowdata-typeto reference built-in XML Schema types such asxs:date,xs:dateTime,xs:time,xs:integer,xs:decimal, andxs:duration. - Collation URI Integration: Rather than relying
solely on
data-typeextensions for advanced text manipulation, modern XSLT specifications recommend using thecollationattribute alongsidedata-type="text"to define language-sensitive, case-order, and natural sorting rules.
Basic Syntax Example
Sorting nodes numerically in an XSLT template:
<xsl:for-each select="product">
<xsl:sort select="price" data-type="number" order="ascending" />
<p><xsl:value-of select="name"/>: $<xsl:value-of select="price"/></p>
</xsl:for-each>