我在使用 XSL 时遇到了一些问题:当它使用 apply-templates 来打印 child 时,是否可以使用另一个模板?我不想使用当前节点,而是真正创建一个与模板匹配的新元素。
我正在搜索的示例:
XML 文件:
<root>
<toto name="foo">
<b>hello</b>
</toto>
</root>
Run Code Online (Sandbox Code Playgroud)
XSL 样式表:
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="tata" name="tata">
<div class="tata">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="toto" name="toto">
<tata>
<xsl:value-of select="@name" />
</tata>
<tata>
<xsl:apply-templates />
</tata>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
预期输出:
<div class="tata">foo</div>
<div class="tata">
<b>hello</b>
</div>
Run Code Online (Sandbox Code Playgroud)