Skip to content

Instantly share code, notes, and snippets.

@jdkirkwood
Created September 17, 2021 14:45
Show Gist options
  • Save jdkirkwood/d5f50c38584cbcb278670afac33c75d0 to your computer and use it in GitHub Desktop.
Save jdkirkwood/d5f50c38584cbcb278670afac33c75d0 to your computer and use it in GitHub Desktop.
XML sort template
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://stackoverflow.com/a/16305113/7643972 -->
<!-- <?xml-stylesheet type="text/xsl" href="../../../sort-xml.xsl"?> -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="text()[not(string-length(normalize-space()))]"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates>
<!-- sort key: "id" attribute on first level elements -->
<xsl:sort select="@id" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment