XSLT <xsl:template>
❮ Complete XSLT Element Reference
定義和用法
當匹配到指定的節點時,<xsl:template> 元素包含要應用的規則。
match 屬性用於將模板與 XML 元素相關聯。match 屬性還可以用於定義 XML 文件整個分支的模板(即 match="/" 定義整個文件)。
注意:<xsl:template> 是一個頂級元素。
語法
<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number">
<!-- Content:(<xsl:param>*,template) -->
</xsl:template>
屬性
Attribute | 值 | 描述 |
---|---|---|
name | name | 可選。為此模板指定一個名稱。 注意:如果省略此屬性,則必須有一個 match 屬性 |
match | pattern | 可選。模板的匹配模式。 注意:如果省略此屬性,則必須有一個 name 屬性 |
mode | mode | 可選。為此模板指定一個模式 |
priority | 數字 | 可選。一個數字,指示模板的數字優先順序 |
示例
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>我的 CD 收藏</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
標題:<span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
藝術家:<span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
❮ Complete XSLT Element Reference