There is given order of nodes and I need to sort the nodes of XML according to the given order. The input is a XML file and output is the XML file with nodes ordered according to given list.
XML:
<root>
<M node="C" home="zzz"/>
<X name="A"/>
<Z/>
<Y test="B"/>
</root>
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pSortingValues" select="'Y,X,Z,M,N'"/>
<xsl:variable name="vSortingValues" select=
"concat(',', $pSortingValues, ',')"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:sort data-type="number" select=
"string-length(substring-before($vSortingValues,concat(',',name,',')))"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Received Output:
<root>
<M node="C" home="zzz"/>
<X name="A"/>
<Z/>
<Y test="B"/>
</root>
Desired Output:
<root>
<Y test="B"/>
<X name="A"/>
<Z/>
<M node="C" home="zzz"/>
</root>
The above XSLT currently only parses the XML without sorting in the required order of nodes: 'Y,X,Z,M,N'.