<xsl:variable>  
変数を定義します。<xsl:variable> をトップレベル要素として指定すると、スタイルシート全体からアクセスできるグローバル変数になります。このように指定しない場合は、ローカル変数となり <xsl:variable> を含む要素内でのみ有効になります。変数の値を定義するには、select 属性で指定するか、または <xsl:variable> 要素内で XSLT テンプレートを定義する 2 つの方法があります。どちらの方法も使用しない場合は、変数の値が空の文字列になります。
 
カテゴリ

トップレベル要素または命令のいずれか。

 
必須の属性
name
この変数の名前を指定する属性。

 
省略可能な属性
select
この変数の値を定義する XPath 式。

 
コンテンツ

<xsl:variable> 要素には何も指定しないか、または XSLT テンプレートを含めることができます。XSLT テンプレートを含める場合、select 属性 (ある場合) は無視されます。

 
指定先

トップレベル要素として <xsl:stylesheet>、またはテンプレート内。

 
定義先

XSLT 11 節「変数とパラメータ」

 

複数の変数を定義するスタイルシートを以下に示します。

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

このスタイルシートを使用して次のドキュメントを変換します。

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

変換の結果は次のとおりです。

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

このスタイルシートには注意する必要がある点がいくつかあります。まず、最初の 2 つの変数 (favoriteNumberfavoriteColor) の値を定義するときに、文字列 "blue" を引用符で囲んでいますが、23 を引用符で囲んでいません。blue を引用符で囲まないと、XSLT プロセッサは現在のコンテキストのすべての <blue> 要素を意味すると判断します。XML 要素名は数字で始めることができないため、23 を引用符で囲む必要はありません。リテラルは、要素名でない場合であっても常に引用符で囲むことをお勧めします。そうすれば、このような仕組みを覚えておく必要がありません。

また、favoriteColor という名前の変数が 2 つあります。1 つは、親が <xsl:stylesheet> 要素であるためグローバル変数です。もう 1 つは、<xsl:template> で定義されているためローカル変数です。match="/" テンプレートの favoriteColor にアクセスした場合の値と、name="processListitems" テンプレート内のこの変数にアクセスした場合の値は異なります。同じレベルの 2 つの変数に同じ名前を指定するとエラーが発生します。同じレベルで同じ名前の <xsl:variable><xsl:param> を定義した場合にもエラーが発生します。

<xsl:choose> 要素を使用して <xsl:variable> を初期化することは一般的な方法です。この方法は、以下のプロシージャプログラミングの構造と同等です。

String complicatedVariable;
if (count(listitems) > 10)
  complicatedVariable = "really long list";
else if (count(listitems)) > 5)
  complicatedVariable = "moderately long list";
else
  complicatedVariable = "fairly short list";

最後に、変数の種類にはノードセットを含む XPath と XSLT があります。processListitems テンプレートを呼び出す場合、渡されたパラメータはドキュメントのすべての <listitem> 要素のノードセットを含む変数です。processListitems テンプレート内では、変数 (この場合、厳密にはパラメータ) を <xsl:for-each> 要素内で使用できます。