是否可以将 XSL 转换的输出存储在某种变量中,然后对变量的内容执行附加转换? (全部在一个 XSL 文件中)
(XSLT-2.0 首选)
XSLT 2.0 解决方案:
<xsl:variable name="firstPassResult">
<xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>
这里的技巧是在第一遍中使用 mode="firstPassResult",而第二遍的所有模板都应具有 mode="secondPass"。
Edit:
例子 :
<root>
<a>Init</a>
</root>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="firstPassResult">
<xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>
<xsl:template match="/" mode="firstPass">
<test>
<firstPass>
<xsl:value-of select="root/a"/>
</firstPass>
</test>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>
<xsl:template match="/" mode="secondPass">
<xsl:message terminate="no">
<xsl:copy-of select="."/>
</xsl:message>
</xsl:template>
</xsl:stylesheet>
输出 :
[xslt] <test><firstPass>Init</firstPass></test>
因此,第一遍创建一些包含 root/a 内容的元素,第二遍将创建的元素打印到 std 输出。希望这足以让您继续前进。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)