<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html> <head> <this-is section="extensions" page="instructions" subpage="assign"/> <!-- Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: saxon:assign</title> <meta name="coverage" content="Worldwide"/> <meta name="copyright" content="Copyright Saxonica Ltd"/> <meta name="title" content="Saxonica: XSLT and XQuery Processing: saxon:assign"/> <meta name="robots" content="noindex,nofollow"/> <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/> </head> <body class="main"> <h1>saxon:assign</h1> <p>The <code>saxon:assign</code> instruction is used to change the value of a global variable that has previously been declared using <code>xsl:variable</code> (or <code>xsl:param</code>). The variable or parameter must be marked as assignable by including the extra attribute <code>saxon:assignable="yes"</code></p> <p><i>This instruction works only with global variables. It should be regarded as deprecated, and may be withdrawn completely at some time in the future, since it is incompatible with many of the optimizations that Saxon now performs.</i></p> <p><i>A better approach to the problem of updateable variables, based on the theory of monads in the functional programming literature, has been described by Dimitre Novatchev in section 3.6 of his paper <a href="http://www.mulberrytech.com/Extreme/Proceedings/html/2003/Novatchev01/EML2003Novatchev01.html" class="bodylink">Functional programming in XSLT using the FXSL library</a>. This approach does not require any XSLT extensions, and the constructs it uses are implemented efficiently in Saxon.</i></p> <p>As with <code>xsl:variable</code>, the name of the variable is given in the mandatory <b>name</b> attribute, and the new value may be given either by an expression in the <code>select</code> attribute, or by expanding the content of the <code>xsl:assign</code> element.</p> <p>If the <code>xsl:variable</code> element has an <code>as</code> attribute, then the value is converted to the required type of the variable in the usual way.</p> <p>Example:</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code> <xsl:variable name="i" select="0" saxon:assignable="yes"/> <xsl:template name="loop"> <saxon:while test="$i &lt; 10"> The value of i is <xsl:value-of select="$i"/> <saxon:assign name="i" select="$i+1"/> </saxon:while> </xsl:template> </code> </pre> </div> <p>The <code>saxon:assign</code> element itself does not allow an <code>as</code> attribute. Instead, it calculates the value of the variable as if <code>as="item()*"</code> were specified. This means that the result of the construct:</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code><saxon:assign name="a">London</saxon:assign></code> </pre> </div> <p>is a single text node, not a document node containing a text node. If you want to create a document node, use <code>xsl:document</code>.</p> <p><b>Note:</b> Using <code>saxon:assign</code> is cheating. XSLT is designed as a language that is free of side-effects, which is why variables are not assignable. Once assignment to variables is allowed, certain optimizations become impossible. At present this doesn't affect Saxon, which generally executes the stylesheet sequentially. However, there are some circumstances in which the order of execution may not be quite what you expect, in which case <code>saxon:assign</code> may show anomalous behavior. In principle the <code>saxon:assignable</code> attribute is designed to stop Saxon doing optimizations that cause such anomalies, but you can't always rely on this.</p> <table width="100%"> <tr> <td> <p align="right"><a class="nav" href="break.xml">Next</a></p> </td> </tr> </table> </body> </html>