I’m unsure how the xsl:sort directive works. I need to sort elements by their tag name (for diffing), and I can’t seem to come up with how to make this work. My first though was to modify the identity transform and just modify it to include a sort statement, but I’m not exactly sure how to do that.
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<!-- xsl:sort ? -->
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
This transformation:
when applied on this XML document:
produces the wanted sorted output:
Do note:
Not only the elements but also the attributes are sorted (the latter is implementation dependent, but works OK with MSXML).
Using sorted XML for diffs is unreliable, because converting an XML document to a sorted representation isn’t 1:1 mapping.