我有一个输入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>
Run Code Online (Sandbox Code Playgroud)
我需要应用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>
Run Code Online (Sandbox Code Playgroud)
唯一的变化是元素中的属性排序更改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>
Run Code Online (Sandbox Code Playgroud)
mytest.xml
<?xml version="1.0" encoding="UTF-8"?>
<attributes> …Run Code Online (Sandbox Code Playgroud)