XSLT <xsl:template>Element


XSLT 元素参考手册Complete XSLT Element Reference Manual

Definition and Usage

The <xsl:template> element contains rules to be applied when a specified node is matched.

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for all branches of an XML document (for example, match="/" defines the entire document).

Note:<xsl:template> is a top-level element.


Syntax

<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number">

<!-- Content:(<xsl:param>*,template) -->

</xsl:template>

Attributes

Attributes Value Description
name name Optional. Defines a name for the template.

Note:If this attribute is omitted, the match attribute must be set.

match pattern Optional. The matching pattern for the template.

Note:If this attribute is omitted, the name attribute must be set.

mode mode Optional. Specifies a mode for the template.
priority number Optional. A number representing the priority number of the template.

Examples

<?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:#0080FF">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

</xsl:stylesheet>

View XML file、 View XSL file、 View result。


XSLT 元素参考手册Complete XSLT Element Reference Manual Other Extensions