XSLT <xsl:template> Element

Definition and Usage

The <xsl:template> element contains the 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 (top-level element).

Syntax

<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number">
  <!-- Content:(<xsl:param>*,template) -->
</xsl:template>

Attribute

Attribute 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 the pattern for the template.
priority number Optional. The priority number of the template.

Instance

Example 1

<?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>