What Does xsl:template Do in XSLT?
The <xsl:template> element is the fundamental
building block of an XSLT (Extensible Stylesheet Language
Transformations) document, defining the transformation rules for
converting specific XML nodes into formatted output such as HTML, plain
text, or another XML structure. By using pattern matching or named
calls, it allows developers to intercept elements or nodes in a source
document and specify the exact structure and content to generate in
response. Understanding how templates match nodes, accept parameters,
and recursively process content is essential for mastering XSLT.
Core Purpose and Functionality
In XSLT, processing is rule-based rather than sequential. The XSLT
processor reads the input XML document as a tree of nodes and searches
the stylesheet for matching <xsl:template> rules to
process each node.
The primary functions of the <xsl:template>
element include:
- Pattern Matching: Intercepting elements, attributes, or text nodes based on XPath expressions.
- Output Generation: Defining literal result elements (like HTML tags) and dynamic values to construct the target document.
- Flow Control: Delegating processing to child nodes
or other specific templates using elements like
<xsl:apply-templates>and<xsl:call-template>. - Reusability: Encapsulating reusable transformation logic that can be invoked across different parts of a stylesheet.
Key Attributes of xsl:template
The behavior of a <xsl:template> element is
largely determined by its attributes:
The match Attribute
The match attribute uses an XPath pattern to associate
the template with specific nodes in the source XML document. When the
XSLT engine traverses the source tree, it applies the template to any
node that satisfies this expression.
<xsl:template match="book">
<div class="book-entry">
<h2><xsl:value-of select="title"/></h2>
<p><xsl:value-of select="author"/></p>
</div>
</xsl:template>In this example, every <book> element encountered
in the source document is transformed into a <div>
container holding heading and paragraph tags.
The name Attribute
The name attribute defines a named template that can be
invoked explicitly, similar to a standard function or subroutine in
procedural programming. Named templates are executed using the
<xsl:call-template> element rather than through
automatic tree matching.
<xsl:template name="formatDate">
<xsl:param name="rawDate"/>
<!-- Transformation logic for date formatting -->
</xsl:template>The mode Attribute
The mode attribute allows developers to process the same
XML nodes multiple times in different ways within the same stylesheet.
For example, a document might process a list of sections once in
mode="toc" to build a table of contents and again without a
mode to generate the full body text.
The priority Attribute
When multiple template rules match the same node, the XSLT processor
resolves the conflict using built-in priority rules. Developers can
override this default resolution by assigning a numeric value to the
priority attribute.
Matching Rules vs. Calling Rules
There are two primary paradigms for executing templates in XSLT:
- Declarative Matching
(
<xsl:apply-templates>): The processor selects nodes from the source tree and searches for the most appropriate<xsl:template match="...">rule. This approach allows stylesheets to handle flexible, recursive, or unpredictable XML structures with minimal boilerplate code. - Procedural Execution
(
<xsl:call-template>): The processor explicitly calls a<xsl:template name="...">block by its identifier, often passing parameters with<xsl:with-param>. This is useful for standalone utility routines and shared UI components.
Template Execution Flow
When an XSLT transformation starts, the processor automatically looks
for a template matching the root node (match="/") of the
source XML document. From there, processing proceeds hierarchically:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Entry point matching the document root -->
<xsl:template match="/">
<html>
<body>
<h1>Library Catalog</h1>
<xsl:apply-templates select="catalog/book"/>
</body>
</html>
</xsl:template>
<!-- Template for individual book items -->
<xsl:template match="book">
<p><xsl:value-of select="title"/> - <xsl:value-of select="price"/></p>
</xsl:template>
</xsl:stylesheet>If no explicit template matches a given node, XSLT applies built-in default templates, which recursively traverse child elements and copy text content directly to the output.
By combining pattern matching, parameterization, and output
construction, <xsl:template> serves as the
foundational mechanism through which all declarative XML transformations
are achieved.