天天看點

XSLT <xsl:variable> 元素

完整的 XSLT 元素參考手冊

定義和用法

<xsl:variable> 元素用于聲明局部或全局的變量。

注意:

如果作為頂層元素(top-level element)來聲明,就是全局變量。如果在模闆内聲明變量,就是局部變量。

一旦您設定了變量的值,就無法改變或修改該值!

提示:您可以通過 <xsl:variable> 元素的内容或通過 select 屬性,向變量添加值!

文法

<xsl:variable

name="name"

select="expression">

<!-- Content:template -->

</xsl:variable>

屬性

描述
name 必需。規定變量的名稱。
select expression 可選。定義變量的值。

執行個體 1

如果設定了 select 屬性,<xsl:variable> 元素就不能包含任何内容。如果 select 屬性含有文字字元串,則必須給字元串加引号。下面的兩個例子為變量 "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: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 />

<xsl:for-each select="table/record">

<xsl:if category="XSL">

</body>

</html>

</xsl:template>

</xsl:stylesheet>

完整的 XSLT 元素參考手冊