<xsl:variable> |
Defines a variable. If <xsl:variable> occurs as a top-level element, it is a global variable that is accessible throughout the stylesheet. Otherwise, the variable is local and exists only in the element that contains the <xsl:variable>. The value of the variable can be defined in one of two ways: specified in the select attribute or defined in an XSLT template inside the <xsl:variable> element itself. If neither method is used, the value of the variable is an empty string.
Category
Either a top-level element or an instruction
Required Attributes
name An attribute that names this variable.
Optional Attributes
select An XPath expression that defines the value of this variable.
Content
The <xsl:variable> element can be empty, or it can contain an XSLT template. If it contains an XSLT template, the value of the select attribute (if any exists) is ignored.
Appears in
<xsl:stylesheet> as a top-level element or in a template.
Defined in
XSLT section 11, Variables and Parameters.
Example
Here is a stylesheet that defines a number of variables:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:output method="text"/>

      <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
      </xsl:variable>

      <xsl:variable name="favoriteNumber" select="23"/>
      <xsl:variable name="favoriteColor" select="'blue'"/>
      <xsl:variable name="complicatedVariable">
        <xsl:choose>
          <xsl:when test="count(//listitem) > 10">
            <xsl:text>really long list</xsl:text>
          </xsl:when>
          <xsl:when test="count(//listitem) > 5">
            <xsl:text>moderately long list</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>fairly short list</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:template match="/">
        <xsl:text>Hello!  Your favorite number is </xsl:text>
        <xsl:value-of select="$favoriteNumber"/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:text>Your favorite color is </xsl:text>
        <xsl:value-of select="$favoriteColor"/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>Here is a </xsl:text>
        <xsl:value-of select="$complicatedVariable"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:variable name="listitems" select="list/listitem"/>
        <xsl:call-template name="processListitems">
          <xsl:with-param name="items" select="$listitems"/>
        </xsl:call-template>
      </xsl:template>

      <xsl:template name="processListitems">
        <xsl:param name="items"/>
        <xsl:variable name="favoriteColor">
          <xsl:text>chartreuse</xsl:text>
        </xsl:variable>

        <xsl:text>    (Your favorite color is now </xsl:text>
        <xsl:value-of select="$favoriteColor"/>
        <xsl:text>.)</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:for-each select="$items">
          <xsl:value-of select="position()"/>
          <xsl:text>.  </xsl:text>
          <xsl:value-of select="."/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>
      </xsl:template>

    </xsl:stylesheet>
We'll use our stylesheet to transform the following document:
    <?xml version="1.0"?>
    <list xml:lang="en">
      <title>Albums I've bought recently:</title>
      <listitem>The Sacred Art of Dub</listitem>
      <listitem>Only the Poor Man Feel It</listitem>
      <listitem>Excitable Boy</listitem>
      <listitem xml:lang="sw">Aki Special</listitem>
      <listitem xml:lang="en-gb">Combat Rock</listitem>
      <listitem xml:lang="zu">Talking Timbuktu</listitem>
      <listitem xml:lang="jz">The Birth of the Cool</listitem>
    </list>
Here are the results of our transformation:
    Hello!  Your favorite number is 23.
    Your favorite color is blue.

    Here is a moderately long list:
        (Your favorite color is now chartreuse.)
    1.  The Sacred Art of Dub
    2.  Only the Poor Man Feel It
    3.  Excitable Boy
    4.  Aki Special
    5.  Combat Rock
    6.  Talking Timbuktu
    7.  The Birth of the Cool
Several things are worth mentioning in our stylesheet. First, notice that when we defined values for the first two variables (favoriteNumber and favoriteColor), we had to quote the string "blue", but didn't have to quote 23. If we don't quote blue, the XSLT processor assumes we mean all the <blue> elements in the current context. We don't have to quote 23 because XML element names can't start with a number. It's a good idea to always quote literals, even those that can't be element names; chances are good that you'll forget this process at some point. Also notice that we have two variables named favoriteColor. One is a global variable because its parent is the <xsl:stylesheet> element; the other is a local variable because it is defined in a <xsl:template>. When we access favoriteColor in the match="/" template, it has one value; when we access it inside the name="processListitems" template, it has another. Having two variables at the same level with the same name is an error. It's also an error to define an <xsl:variable> and an <xsl:param> with the same name at the same level. Using an <xsl:choose> element to initialize an <xsl:variable> is a common technique. This technique is the equivalent of this procedural programming construct:
    String complicatedVariable;
    if (count(listitems) > 10)
      complicatedVariable = "really long list";
    else if (count(listitems)) > 5)
      complicatedVariable = "moderately long list";
    else
      complicatedVariable = "fairly short list";
The last point we'll make is that a variable can be any of the XPath or XSLT variable types, including a node-set. When we call the processListitems template, the parameter we pass to it is a variable containing the node-set of all the <listitem> elements in our document. Inside the processListitems template, our variable (which is now technically a parameter) can be used inside an <xsl:for-each> element.