要達到此目的,您可能需要使用擴展功能,即節點集函數,該函數從結果樹片段返回一組節點。
首先,您需要定義命名空間的擴展功能,像這樣
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
在這種情況下,我使用的是微軟的擴展功能,但其他人根據您所使用的平臺是可用的。 (對於非Microsoft平臺,http://exslt.org/common是另一種常見的)。
然後,您可以訪問的元素在變量像這樣(作爲一個例子)
<xsl:apply-templates select="msxsl:node-set($aTree)/foos/foo"/>
在一個簡單的例子,這完全把給你這個
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:variable name="mytree">
<foos>
<foo>bar1</foo>
<foo>bar2</foo>
<foo>bar3</foo>
<foo>bar4</foo>
</foos>
</xsl:variable>
<xsl:template match="/">
<xsl:call-template name="myTemplate">
<xsl:with-param name="aTree" select="$mytree"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="myTemplate">
<xsl:param name="aTree"/>
<newfoos>
<xsl:apply-templates select="msxsl:node-set($aTree)/foos/foo"/>
</newfoos>
</xsl:template>
<xsl:template match="foo">
<newfoo>
<xsl:value-of select="text()"/>
</newfoo>
</xsl:template>
</xsl:stylesheet>
運行時,這只是輸出以下結果:
<newfoos>
<newfoo>bar1</newfoo>
<newfoo>bar2</newfoo>
<newfoo>bar3</newfoo>
<newfoo>bar4</newfoo>
</newfoos>
鑑於此示例,沒有理由您無法首先動態創建您的myTree變量。
好的,謝謝你的工作 – 2011-05-31 15:21:36