Sort specific xml tags by attribute value



Say I have the following xml structure:



<a>
<t attr="27"></t>
<t attr="23"></t>
<b id="a"></b>
<b id="t"></b>
<b id="p"></b>
<c attr="er"></c>
</a>


And I want to create a new xml file with only the b tags sorted by attribute id, and leave the rest of the file unchanged:



<a>
<t attr="27"></t>
<t attr="23"></t>
<b id="a"></b>
<b id="p"></b>
<b id="t"></b>
<c attr="er"></c>
</a>


How can I do this?


I came up with this for the sorting part, but I would like the tags on separate lines and I do not know how to copy the rest of the file:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">

<xsl:template match="a">
<xsl:apply-templates select="b">
<xsl:sort select="@name" order="ascending" data-type="text" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="b">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment