SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Notices
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
XPath API for Java
Saxon on .NET
Extensibility
Saxon Extensions
| Introduction
| EXSLT Extensions
| Extension attributes (XSLT only)
| Additional serialization parameters
| Extension functions
» | Extension instructions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
XPath 2.0 Expression Syntax
Function Library
Standards Conformance

# saxon:assign

The `saxon:assign` instruction is used to change the value of a global variable that has previously been declared using `xsl:variable` (or `xsl:param`). The variable or parameter must be marked as assignable by including the extra attribute `saxon:assignable="yes"`

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.

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 Functional programming in XSLT using the FXSL library. This approach does not require any XSLT extensions, and the constructs it uses are implemented efficiently in Saxon.

As with `xsl:variable`, the name of the variable is given in the mandatory name attribute, and the new value may be given either by an expression in the `select` attribute, or by expanding the content of the `xsl:assign` element.

If the `xsl:variable` element has an `as` attribute, then the value is converted to the required type of the variable in the usual way.

Example:

    <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>

The `saxon:assign` element itself does not allow an `as` attribute. Instead, it calculates the value of the variable as if `as="item()*"` were specified. This means that the result of the construct:

    <saxon:assign name="a">London</saxon:assign>

is a single text node, not a document node containing a text node. If you want to create a document node, use `xsl:document`.

Note: Using `saxon:assign` 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 `saxon:assign` may show anomalous behavior. In principle the `saxon:assignable` attribute is designed to stop Saxon doing optimizations that cause such anomalies, but you can't always rely on this.

Next