我有一个file.xml
<?xml version="1.0"?>
<Report>
<row>
<field1>test1</field1>
<field2>test2</field2>
<field3>test3</field3>
</row>
<row>
<field1>test4</field1>
<field2>test5</field2>
<field3>test6</field3>
</row>
</Report>
Run Code Online (Sandbox Code Playgroud)
还有一个lookup.xml
<?xml version="1.0"?>
<lookup>
<field1>fieldA</field1>
<field2>fieldB</field2>
<field3>fieldC</field3>
</lookup>
Run Code Online (Sandbox Code Playgroud)
我想获得以下输出
<?xml version="1.0"?>
<Report>
<row>
<fieldA>test1</fieldA>
<fieldB>test2</fieldB>
<fieldC>test3</fieldC>
</row>
<row>
<fieldA>test4</fieldA>
<fieldB>test5</fieldB>
<fieldC>test6</fieldC>
</row>
</Report>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我提出了以下transform.xsl
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:variable name="lookupDoc" select="document('lookup.xml')"/>
<xsl:template match="Report">
<Items>
<xsl:apply-templates/>
</Items>
</xsl:template>
<xsl:template match="row">
<Item>
<xsl:apply-templates/>
</Item>
</xsl:template>
<xsl:template match="row/*">
<xsl:variable name="this" select="."/>
<xsl:variable name="lookup">
<xsl:for-each select="$lookupDoc">
<xsl:key name="k1" match="local-name()" use="text()"/>
<xsl:value-of select="key('k1', local-name($this))"/>
</xsl:for-each>
</xsl:variable>
<fieldName name="{$lookup}">
<xsl:value-of select="."/>
</fieldName>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
Xsl的新功能因此确定了为什么会出现编译器错误
看起来你有正确的想法(并且有一个新颖的想法),但有些地方需要修复.请试试这个:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:key name="k1" match="*" use="local-name()"/>
<xsl:variable name="lookupDoc" select="document('lookup.xml')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="row/*">
<xsl:variable name="newName">
<xsl:apply-templates select="$lookupDoc/lookup">
<xsl:with-param name="nameToMatch" select="local-name()" />
</xsl:apply-templates>
</xsl:variable>
<xsl:element name="{$newName}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="lookup">
<xsl:param name="nameToMatch" />
<xsl:value-of select="string(key('k1', $nameToMatch))"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
为了key()在$lookupDocDOM中定位值,key()需要在该DOM 的上下文中使用,这就是最后一个模板的用途.当您在样本输入上运行此结果时,结果是您请求的输出:
<Report>
<row>
<fieldA>test1</fieldA>
<fieldB>test2</fieldB>
<fieldC>test3</fieldC>
</row>
<row>
<fieldA>test4</fieldA>
<fieldB>test5</fieldB>
<fieldC>test6</fieldC>
</row>
</Report>
Run Code Online (Sandbox Code Playgroud)
通过一些修改,也可以使用for-each您尝试使用的方法,因为这是进入$lookupDocDOM 的另一种方法.以下XSLT应该与上面的XSLT具有相同的结果,并且与您原来的尝试更相似:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:key name="k1" match="*" use="local-name()"/>
<xsl:variable name="lookupDoc" select="document('lookup.xml')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="row/*">
<xsl:variable name="this" select="." />
<xsl:variable name="lookup">
<xsl:for-each select="$lookupDoc">
<xsl:value-of select="key('k1', local-name($this))"/>
</xsl:for-each>
</xsl:variable>
<xsl:element name="{$lookup}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)