<?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 2011-12-09T20:47:22.916Z--><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><xsl:variable name="title">A really exciting document"</xsl:variable> <xsl:variable name="backcolor" expr="'#FFFFCC'" /> <xsl:template match="/*"> <HTML><TITLE<xsl:value-of select="$title"/></TITLE> <BODY BGCOLOR='{$backcolor}'> ...<br/> </BODY></HTML> </xsl:template></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>