是否可以将XSL转换的输出存储在某种类型的变量中,然后对变量的内容执行额外的转换?(所有内容都在一个XSL文件中)
(优先考虑XSLT-2.0)
发布于 2011-11-22 01:35:45
XSLT2.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”。
编辑:
示例:
<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输出。希望这足以让你继续前进。
发布于 2011-11-22 01:39:09
是的,使用XSLT 2.0很容易。在XSLT1.0中,当然也可以使用模式并将临时结果存储在变量中,方法与在XSLT2.0中相同,但是变量是结果树片段,为了能够使用apply-templates进一步处理它,需要在变量上使用exsl:node-set等扩展函数。
https://stackoverflow.com/questions/8215892
复制相似问题