XSLT <xsl:template> 元素

定義和用法
<xsl:template> 元素包含了當匹配指定節點時要應用的規則。
match 屬性用於把範本關聯到某個 XML 元素。match 屬性也能用於為 XML 文檔的全部分支定義範本(比如,match="/" 定義了整個文檔)。
注意:<xsl:template> 是頂層元素(top-level element)。
語法
<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number">
<!-- Content:(<xsl:param>*,template) -->
</xsl:template>
name="name"
match="pattern"
mode="mode"
priority="number">
<!-- Content:(<xsl:param>*,template) -->
</xsl:template>
屬性
屬性 | 值 | 描述 |
---|---|---|
name | name | 可選。為範本定義名稱。 注釋:如果省略該屬性,則必須設置 match 屬性。 |
match | pattern | 可選。範本的匹配模式。 注釋:如果省略該屬性,則必須設置 name 屬性。 |
mode | mode | 可選。為範本規定模式。 |
priority | number | 可選。一個表示範本的優先順序編號的數字。 |
實例
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</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">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</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">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
