I am trying to sort an XML file using XSLT transformation, but it doesn't do what I want. I want to sort the XML lexicographically by the loc
element.
This is my original XML:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>url3</loc>
</url>
<url>
<loc>url2</loc>
</url>
<url>
<loc>url1</loc>
</url>
</urlset>
This is the xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"
version="1.0" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="urlset">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="url">
<xsl:sort select="loc" data-type="text" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And I'd like to see this output:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>url1</loc>
</url>
<url>
<loc>url2</loc>
</url>
<url>
<loc>url3</loc>
</url>
</urlset>
I tried to follow this question, but I can't get it working.
The XSLT above just copies the original xml without any sorting and that's it. Can somebody help me? How can I sort the loc
tags?