我有两个XML文件。一个是主XML文件,另一个用作查找表。以下是主要的XML:
<Report>
<row>
<field1>test1</field1>
<field2>test2</field2>
<field3>test3</field3>
</row>
<row>
<field1>test4</field1>
<field2>test5</field2>
<field3>test6</field3>
</row>
</Report>
查找xml文件如下所示:
<lookup>
<fieldmapping name="field1">fieldA</fieldmapping>
<fieldmapping name="field2">fieldB</fieldmapping>
<fieldmapping name="field3">fieldC</fieldmapping>
</lookup>
以下是我想要的输出xml:
<Items>
<Item>
<FieldName name="fieldA">test1</FieldName>
<FieldName name="fieldB">test2</FieldName>
<FieldName name="fieldC">test3</FieldName>
</Item>
<Item>
<FieldName name="fieldA">test4</FieldName>
<FieldName name="fieldB">test5</FieldName>
<FieldName name="fieldC">test6</FieldName>
</Item>
</Items>
我正在使用以下XSLT,但不知道如何从field1、field2和field3中选择值:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="mappingLookupDoc" select="document( lookup.xml )/lookup/fieldmapping "/>
<xsl:key name="mappingKey" match="fieldmapping " use="@name"/>
<xsl:template match="report">
<xsl:apply-templates select="$mappingLookupDoc"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<Items xmlns="http://www.w3.org/1999/xhtml" schemaVersion="1.0">
<xsl:for-each select="report/row">
<Item>
<xsl:for-each select="$mappingLookupDoc">
<xsl:variable name="fieldname" select="@name"/>
<FieldName>
<xsl:attribute name="name">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:value-of select="/report/row/?????"/>
</FieldName>
</xsl:for-each>
</Item>
</xsl:for-each>
</Items>
</xsl:template>