XSLT <xsl:variable>
❮ Complete XSLT Element Reference
定義和用法
元素 <xsl:variable> 用於宣告一個區域性或全域性變數。
注意:如果變數是作為頂級元素宣告的,則它是全域性的;如果它是在模板內宣告的,則是區域性的。
注意:一旦設定了變數的值,就不能更改或修改該值!
提示:您可以透過 <xsl:variable> 元素的內容 **或** 透過 select 屬性為變數新增值!
語法
<xsl:variable
name="name"
select="expression">
<!-- Content:template -->
</xsl:variable>
屬性
Attribute | 值 | 描述 |
---|---|---|
name | 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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="header">
<tr bgcolor="#9acd32">
<th>標題</th>
<th>藝術家</th>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:copy-of select="$header" />
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
❮ Complete XSLT Element Reference