这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:reps>
<rep pos="1">STREET</rep>
<rep pos="1">CITY</rep>
<rep pos="1">STATE</rep>
<rep pos="1">ZIP</rep>
</my:reps>
<xsl:variable name="vReps"
select="document('')/*/my:reps/*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ADDRESS">
<xsl:variable name="vPos" select=
"count(preceding-sibling::ADDRESS)+1"/>
<xsl:element name="{$vReps[position()=$vPos]}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<ROOT>
<RECORD>
<ID>A1</ID>
<NAME>Name1</NAME>
<ADDRESS>StreetName</ADDRESS>
<ADDRESS>CityName</ADDRESS>
<ADDRESS>State</ADDRESS>
<ADDRESS>321</ADDRESS>
<PHONE>12345</PHONE>
</RECORD>
<RECORD>
<ID>A1</ID>
<NAME>Name1</NAME>
<ADDRESS>StreetName</ADDRESS>
<ADDRESS>CityName</ADDRESS>
<ADDRESS>State</ADDRESS>
<ADDRESS>123</ADDRESS>
<PHONE>12345</PHONE>
</RECORD>
</ROOT>
产生想要的正确结果:
<ROOT>
<RECORD>
<ID>A1</ID>
<NAME>Name1</NAME>
<STREET>StreetName</STREET>
<CITY>CityName</CITY>
<STATE>State</STATE>
<ZIP>321</ZIP>
<PHONE>12345</PHONE>
</RECORD>
<RECORD>
<ID>A1</ID>
<NAME>Name1</NAME>
<STREET>StreetName</STREET>
<CITY>CityName</CITY>
<STATE>State</STATE>
<ZIP>123</ZIP>
<PHONE>12345</PHONE>
</RECORD>
</ROOT>
说明:
用模板匹配覆盖标识规则,ADDRESS
并根据该元素的位置生成一个ADDRESS
元素。