How can I use XSLT to control attribute ordering?
I have an input XML document:
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<allNames id="ID_0" b:type="a:UnstructuredName">
<typeName>KnownBy</typeName>
<startDate>2001-01-01-05:00</startDate>
<fullName>ABCD 004 COMPANY INC</fullName>
</allNames>
I require to apply an XSLT to convert this to
Output XML
<?xml version="1.0" encoding="UTF-8"?>
<allNames b:type="a:UnstructuredName" id="ID_0">
<typeName>KnownBy</typeName>
<startDate>2001-01-01-05:00</startDate>
<fullName>ABCD 004 COMPANY INC</fullName>
</allNames>
The only change is the attribute ordering change in the allNames
element. I looked up another post and wrote XSLT that orders attributes, but I do not know how to get the whole thing working.
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="attributes" select="document('mytest.xml')//attribute"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:variable name="self" select="."/>
<xsl:for-each select="$attributes">
<xsl:apply-templates select="$self/@*[name()=current()]"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
mytest.xml
<?xml version="1.0" encoding="UTF-8"?>
<attributes>
<attribute>b:type</attribute>
<attribute>id</attribute>
</attributes>