XSLT を使用して属性の順序を制御するにはどうすればよいですか?
入力 XML ドキュメントがあります。
入力 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>
これを変換するには、XSLT を適用する必要があります。
出力 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>
唯一の変更点は、要素内の属性の順序変更allNames
です。別の投稿を調べて、属性を注文する XSLT を書きましたが、全体を機能させる方法がわかりません。
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>