Dec 19, 2008

XSLT note 3 - variable

You can basically build a variable just as building any output. The simple way is


But you don't have to use select. Yiou can construct your variable in the constructor. Following is some sample


  
 
  
   
  
 



 

This is demo

How to use variable

If a variable contains a set of nodes. you can use the variable in other XPath expressions without any limitations. For example,






If you are using constructor to build a constructor, the variable can hold any arbitrary content, this content is called result tree fragment. You can imagine a result tree fragment (RTF) as a fragment or a chunk of XML code. You can assign a result tree fragment to a variable directly, or result tree fragment can arise from applying templates or other XSLT instructions. The following code assigns a simple fragment of XML to the variable $author.


  Jirka
  Kosek
  jirka@kosek.cz

Now let's say we want to extract the e-mail address from the $author variable. The most obvious way is to use an expression such as $author/email. But this will fail, as you can't apply XPath navigation to a variable of the type "result tree fragment."


  ...
  
  
  ...

If you don't reuse the content of result tree, you can put copy-of statement to where you want your output to be.


variable has a scope, like the following example shows, the $castList is referenced outside of the definition scope, so it is illegal.