这是一个与 XSLT 1.0 兼容的选项:
样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<!--
Pass the name of the other XML file as a parameter to the transformation;
x1.xml is used by default
-->
<xsl:param name="x1" select="'x1.xml'"/>
<xsl:template match="/">
<!-- Only apply the first <c> element -->
<xsl:apply-templates
select="DocumentElement/c[XMLFieldName = 'UserDetails']"/>
</xsl:template>
<xsl:template match="c[XMLFieldName = 'UserDetails']">
<!-- Use the value of the XMLFieldName element as the element name -->
<xsl:element name="{XMLFieldName}">
<!-- Apply following <c> siblings -->
<xsl:apply-templates select="following-sibling::c"/>
</xsl:element>
</xsl:template>
<xsl:template match="c">
<!-- Save the value of the ID child of current <c> element -->
<xsl:variable name="id" select="ID"/>
<xsl:element name="{XMLFieldName}">
<!--
Get the value of the element in x1.xml that has the same number in its
element name as the value of the <ID> child of this <c> element
-->
<xsl:value-of
select="document($x1)/*/*[substring-after(local-name(), 'x') = $id]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
输入
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<c>
<ID>1</ID>
<XMLFieldName>UserDetails</XMLFieldName>
</c>
<c>
<ID>2</ID>
<XMLFieldName>UserName</XMLFieldName>
</c>
<c>
<ID>3</ID>
<XMLFieldName>FirstName</XMLFieldName>
</c>
<c>
<ID>4</ID>
<XMLFieldName>LastName</XMLFieldName>
</c>
<c>
<ID>5</ID>
<XMLFieldName>EmployeeID</XMLFieldName>
</c>
</DocumentElement>
x1.xml
<x1>
<x2>xyz</x2>
<x3>xy</x3>
<x4>z</x4>
<x5>123</x5>
</x1>
输出
<?xml version="1.0" encoding="utf-8"?>
<UserDetails>
<UserName>xyz</UserName>
<FirstName>xy</FirstName>
<LastName>z</LastName>
<EmployeeID>123</EmployeeID>
</UserDetails>