<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="xsl-elements" page="variable" subpage=""/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: xsl:variable</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:variable"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>xsl:variable</h1>
      <p>The <code>xsl:variable</code> element is used to declare a variable and give it a value. If it appears at the
top level (immediately within xsl:stylesheet) it declares a global variable, otherwise it declares a local variable
that is visible only within the stylesheet element containing the xsl:variable declaration.</p>
      <p>The mandatory <code>name</code> attribute defines the name of the variable.</p>
      <p>The value of the variable may be defined either by an expression within the optional <code>select</code> attribute, or
by the contents of the xsl:variable element. In the latter case the result is a temporary tree. A
temporary tree can be used like a source document, for example it can be accessed using path expressions
and processed using template rules.</p>
      <p>There is an optional attribute, <code>as</code>, to define the type of the variable. The actual supplied
value must be an instance of this type; it will not be converted. <i>This has changed in Saxon 7.4: previous
releases did a conversion.</i></p>
      <p>In standard XSLT, variables once declared cannot be updated. Saxon however provides a 
<a class="bodylink" href="../extensions/instructions/assign.xml">saxon:assign</a>
 extension element to circumvent this restriction.</p>
      <p>The value of a variable can be referenced within an expression using the syntax <code>$name</code>.</p>
      <p>Example:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;xsl:variable name="title"&gt;A really exciting document"&lt;/xsl:variable&gt;
&lt;xsl:variable name="backcolor" expr="'#FFFFCC'" /&gt;
&lt;xsl:template match="/*"&gt;
    &lt;HTML&gt;&lt;TITLE&lt;xsl:value-of select="$title"/&gt;&lt;/TITLE&gt;
    &lt;BODY BGCOLOR='{$backcolor}'&gt;
    ...<br/>
    &lt;/BODY&gt;&lt;/HTML&gt;
&lt;/xsl:template&gt;</code>
         </pre>
      </div>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="when.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>