XSLT <xsl:variable>要素
完全なXSLT要素リファレンス
定義と使用法
<xsl:variable> 要素は、ローカルまたはグローバルな変数を宣言するために使用します。
注意:トップレベル要素として宣言すると、グローバル変数になります。テンプレート内で変数を宣言すると、ローカル変数になります。
注意:変数の値を設定すると、その値を変更または修正することはできません。
ヒント:<xsl:variable> 要素の内容または select 属性によって、変数に値を追加できます。
構文
<xsl:variable
name="name"
select="expression">
<!-- Content:template -->
</xsl:variable>
name="name"
select="expression">
<!-- Content:template -->
</xsl:variable>
属性
| 属性 | 值 | 説明 |
|---|---|---|
| name | name | 必須。変数の名前を指定します。 |
| select | expression | 任意。変数の値を定義します。 |
例 1
select 属性が設定されている場合、<xsl:variable> 要素は内容を含めることができません。select 属性に文字列が含まれる場合は、その文字列を引用符で囲む必要があります。次の2つの例では、変数 "color" に "red" を代入します:
<xsl:variable name="color" select="'red'" />
<xsl:variable name="color" select='"red"' />
例 2
<xsl:variable> 要素が name 属性のみを含み、内容がない場合、変数の値は空文字列になります:
<xsl:variable name="j" />
例 3
次の例では、<xsl:variable> 要素の内容を使用して変数 "header" に値を代入します:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="header">
<tr>
<th>Element</th>
<th>Description</th>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:copy-of select="$header" />
<xsl:for-each select="reference/record">
<tr>
<xsl:if category="XML">
<td><xsl:value-of select="element"/></td>
<td><xsl:value-of select="description"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
<br />
<table>
<xsl:copy-of select="$header" />
<xsl:for-each select="table/record">
<tr>
<xsl:if category="XSL">
<td><xsl:value-of select="element"/></td>
<td><xsl:value-of select="description"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="header">
<tr>
<th>Element</th>
<th>Description</th>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:copy-of select="$header" />
<xsl:for-each select="reference/record">
<tr>
<xsl:if category="XML">
<td><xsl:value-of select="element"/></td>
<td><xsl:value-of select="description"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
<br />
<table>
<xsl:copy-of select="$header" />
<xsl:for-each select="table/record">
<tr>
<xsl:if category="XSL">
<td><xsl:value-of select="element"/></td>
<td><xsl:value-of select="description"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
完全なXSLT要素リファレンス その他の拡張