如何在xslt中构造变量中的树
我想创建这样一个变量:如何在xslt中构造变量中的树,xslt,Xslt,我想创建这样一个变量: <xsl:variable name="mytree" > <foos> <foo>bar</foo> <foo>bar</foo> <foo>bar</foo> <foo>bar</foo> <foos> </xsl:variable> 酒吧
<xsl:variable name="mytree" >
<foos>
<foo>bar</foo>
<foo>bar</foo>
<foo>bar</foo>
<foo>bar</foo>
<foos>
</xsl:variable>
酒吧
酒吧
酒吧
酒吧
要像在中一样使用它,请执行以下操作:
<xsl:call-template name="myTemplate">
<xsl:with-param name='aTree' select='$mytree' />
</xsl:call-template>
<xsltemplate name="myTemplate">
<xsl:param name="aTree" />
[My code that treat $aTree like a Tree]
</xsl:template>
[我的代码将$aTree视为一棵树]
我的问题是:是否可能创建树变量以及如何创建?要实现这一点,您可能需要使用扩展函数,即节点集函数,该函数从结果树片段返回一组节点 首先需要为扩展函数定义名称空间,如下所示
<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 是非Microsoft平台的另一个常见问题)
然后,您可以像这样访问变量中的元素(例如)
把这些放在一个简单的例子中,你会发现
<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>
bar1
bar2
三维直方图
bar4
运行时,只会输出以下结果:
<newfoos>
<newfoo>bar1</newfoo>
<newfoo>bar2</newfoo>
<newfoo>bar3</newfoo>
<newfoo>bar4</newfoo>
</newfoos>
bar1
bar2
三维直方图
bar4
在本例中,没有理由不能首先动态创建myTree变量。要实现这一点,可能需要使用扩展函数,即节点集函数,该函数从结果树片段返回一组节点 首先需要为扩展函数定义名称空间,如下所示
<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 是非Microsoft平台的另一个常见问题)
然后,您可以像这样访问变量中的元素(例如)
把这些放在一个简单的例子中,你会发现
<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>
bar1
bar2
三维直方图
bar4
运行时,只会输出以下结果:
<newfoos>
<newfoo>bar1</newfoo>
<newfoo>bar2</newfoo>
<newfoo>bar3</newfoo>
<newfoo>bar4</newfoo>
</newfoos>
bar1
bar2
三维直方图
bar4
在本例中,没有理由不首先动态创建myTree变量