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>
Run Code Online (Sandbox Code Playgroud)
这里的诀窍是第一次使用mode ="firstPassResult",而sedond pass的所有模板都应该有mode ="secondPass".
编辑:
示例:
<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>
Run Code Online (Sandbox Code Playgroud)
输出:
[xslt] <test><firstPass>Init</firstPass></test>
Run Code Online (Sandbox Code Playgroud)
因此,第一遍创建一些元素,其中包含root/a的内容,第二遍将打印创建的元素打印到std out.希望这足以让你前进.