<p>
You can build a variable by using select like below.
</p>
<pre data-sub="prettyprint:_">
<xsl:variable name="v" select="xpath-expression" />
</pre>
<p>Or you can build a variable using constructor like below </p>
<pre data-sub="prettyprint:_">
<xsl:variable name="Subtotals">
<!-- consturctor: can be any markup -->
<xsl:for-each select="Row">
<number>
<xsl:value-of select="Quantity * Price"/>
</number>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="header">
<h1 style="color:red;">This is demo</h1>
</xsl:variable>
<xsl:variable name="tmpResult">
<xsl:apply-templates select="$orderRow" />
</xsl:variable>
</pre>
<p>
If a variable initialized by a select course, it contains a set of nodes. You can use the variable as a node. For example,
</p>
<pre data-sub="prettyprint:_">
<xsl:variable name="books" select="//book"/>
<xsl:for-each select="$books/author">
</xsl:for-each>
<!-- same as
<xsl:for-each select="//book/author">
</xsl:for-each>
-->
</pre>
<p>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. </p>
<pre data-sub="prettyprint:_">
<xsl:variable name="author">
<firstname>Jirka</firstname>
<surname>Kosek</surname>
<email>jirka@kosek.cz</email>
</xsl:variable>
</pre>
<p>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."</p>
<pre data-sub="prettyprint:_">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
extension-element-prefixes="exsl"
version="1.0">
...
<!-- Now we can convert result tree fragment back to node-set -->
<xsl:value-of select="msxsl:node-set($author)/email"/>
...
</xsl:stylesheet>
</pre>
<p>
If you want to reuse the content of result tree, you can put copy-of statement to where you want your output to be.
</p>
<pre data-sub="prettyprint:_">
<xsl:copy-of select="$header"/>
</pre>