开发者

How contruct in xslt a tree in a variable

I want to create a variable like that :

<xsl:variable name="mytree" >
     <foos>
        <foo>bar</foo>
        <foo>bar</foo>
        <foo>bar</foo>
        <foo>bar</foo>
     <foos>
</xsl:variable>

to use it like in :

<xsl:call-template name="myTemplate">
<xsl:with-param name='aTree' select='$mytree' />
</xsl:call-template>

<xsltemplate name="myTemplate">
<xsl:param name="aTree" /&g开发者_如何学JAVAt;
[My code that treat $aTree like a Tree]
</xsl:template>

My question is : is it possible to create a Tree variable and How?


To achieve this, you probably need to make use of an extension function, namely the node-set function, which returns a set of nodes from a result tree fragment.

First you would need to define the namespace for the extension functions like so

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt">

In this case, I am using the Microsoft extension functions, but others are available depending on which platform you are using. (http://exslt.org/common is another common one for non-Microsoft platforms).

Then, you can access the elements in your variable like so (as an example)

<xsl:apply-templates select="msxsl:node-set($aTree)/foos/foo"/>

Putting this altogether in a simple example gives you this

<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>

When run, this simply outputs the following result:

<newfoos>
   <newfoo>bar1</newfoo>
   <newfoo>bar2</newfoo>
   <newfoo>bar3</newfoo>
   <newfoo>bar4</newfoo>
</newfoos>

Given this example, there is no reason why you can't dynamically create your myTree variable first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜