我是 XSLT 转换的新手,并且陷入了这种递归映射的困境。
<Element1>
<Element11/>
<Element12/>
<Element13/>
<Element1>
<Element11/>
<Element12/>
<Element13/>
</Element1>
</Element1>
将化身为
<Information>
<Element11/>
<Element12/>
<Element13/>
</Information>
<!-- This will be the child Element1 -->
<Metadata>
<Element11/>
<Element12/>
<Element13/>
</Metadata>
我绝对不能使用:
<xsl:template match="/">
<xsl:for-each select="Element1">
<Information>
</xsl:for-each>
</xsl:template>
这应该可以完成这项工作:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Copy everything -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Template handling the top-level 'Element1' -->
<xsl:template match="Element1">
<Information>
<!-- Apply the copy template to all sub-elements except 'Element1' -->
<xsl:apply-templates select="*[name()!='Element1']"/>
</Information>
<!-- Apply the templates to the 'Element1' sub-elements -->
<xsl:apply-templates select="Element1"/>
</xsl:template>
<!-- Template handling the inner 'Element1' -->
<xsl:template match="Element1/Element1">
<Metadata>
<xsl:apply-templates/>
</Metadata>
</xsl:template>
</xsl:stylesheet>
正如 Tim 指出的那样,结果不是有效的 XML,因为它有两个根元素。生成额外的root
元素以使输出有效 XML 添加此模板:
<xsl:template match="/">
<root>
<xsl:apply-templates></xsl:apply-templates>
</root>
</xsl:template>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)