xsl:template
Defines a template rule that fires when its match pattern matches a node, or a named template that is invoked explicitly.
<xsl:template match="book">
...
</xsl:template>A template is the basic unit of an XSLT stylesheet. Match templates are selected automatically by the processor when xsl:apply-templates reaches a matching node; named templates are called explicitly with xsl:call-template. A template can be both — it can have a match and a name attribute. The body of a template is a sequence constructor that produces nodes (or atomic values, with as) added to the result tree.
When several match templates apply to the same node, the one with the highest import precedence, then the highest priority, wins. Priority can be set explicitly with priority or is computed from the pattern (e.g. a name test has higher priority than node()).
Attributes
| Attribute | Description |
|---|---|
match |
Pattern selecting the nodes this template applies to. Required unless name is given. |
name |
QName making this a named template, callable via xsl:call-template. |
priority |
Number overriding the pattern’s default priority for conflict resolution. |
mode |
Space-separated list of mode names (or #default, #all) the template belongs to. |
as |
Sequence type the template’s result is coerced to. |
Examples
A named template called from elsewhere:
<xsl:template name="header">
<xsl:param name="title"/>
<h1><xsl:value-of select="$title"/></h1>
</xsl:template>
<xsl:call-template name="header">
<xsl:with-param name="title" select="'Hello'"/>
</xsl:call-template>A template restricted to a mode, with explicit priority:
<xsl:template match="section" mode="toc" priority="2">
<li><xsl:value-of select="title"/></li>
</xsl:template>
<xsl:apply-templates select="//section" mode="toc"/>Returning a typed value instead of writing to the result tree:
<xsl:template name="square" as="xs:integer">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="$n * $n"/>
</xsl:template>