変数とパラメータ


変数の宣言

xsl:variable 要素、パラメータの宣言は xsl:param 要素を用いて行います。変数およびパラメータの値は式の中で参照することができます。変数とパラメータは、 パラメータの場合には XSLT スタイルシートで結び付けられる値がデフォルト値にすぎない、 という点で異なります。 つまり、XSLT スタイルシートやテンプレートを呼び出す際にパラメータに値を渡すことができます。

2番目の要素を指定するには以下のようにします。

<xsl:variable name="n" select="2"></xsl:variable>
<xsl:template match="people">
  <xsl:value-of select="person[$n]"/>
</xsl:template>
<xsl:variable name="n">2</xsl:variable>
<xsl:template match="people">
  <xsl:value-of select="person[position()=$n]"/>,
</xsl:template>

文字列を代入するには

タグ要素と解釈されないために、下記の様にすればうまくいった

<xsl:variable name="n" select="string('asdf')"></xsl:variable>~
<xsl:value-of select="$n"/>~

テンプレートへのパラメータの引き渡し

<xsl:template match="/">
  <xsl:apply-templates select="people">
    <xsl:with-param name="other_person" select="people/person[1]" />
  </xsl:apply-templates>
</xsl:template>
<xsl:template match="people">
  <xsl:param name="other_person">Bobby Gillespie</xsl:param>
  <xsl:value-of select="$other_person"/>
</xsl:template>