2009-09-17

XSLT script to sort nodes alphabetically

The simple XSL sample you have been looking for. Works great with Saxon. "version" set to 2.0 here to prevent a warning with the latest version of Saxon, but you can change it to 1.0 for older XSLT engines.
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="@typeName"/>
<xsl:sort select="name(.)"/>
<xsl:sort />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

Oh, and the saxon commandline syntax to apply an XSL (which should really be provided in a straightforward fashion in the commandline help) is:
C:\>saxon -s:banks.xml -xsl:sort.xsl -o:banks_sorted.xml

No comments:

Post a Comment

Note: only a member of this blog may post a comment.