XSLT Template match vs name Attributes Explained
In Extensible Stylesheet Language Transformations (XSLT), the
<xsl:template> element relies on either the
match or name attribute to define how rules
are triggered and executed. While the match attribute
enables a declarative, pattern-matching approach invoked by
<xsl:apply-templates>, the name
attribute provides a procedural, function-style mechanism executed
explicitly through <xsl:call-template>. Understanding
the functional differences between these two attributes determines
whether your stylesheet operates on a dynamic “push” model or a direct
“pull” model.
The
match Attribute: Pattern-Driven Processing
The match attribute defines an XPath pattern that
identifies specific nodes in the input XML document. When the XSLT
processor encounters <xsl:apply-templates>, it scans
the stylesheet for a template whose match pattern
corresponds to the current node being processed.
- Invocation: Implicitly triggered via
<xsl:apply-templates />. - Execution Style: Declarative and rule-based (Push Model).
- Context Node: The processor automatically sets the context node to the matching node in the source document.
- Primary Use Case: Transforming hierarchical document structures where the order and frequency of elements can vary.
<!-- Template matches any <book> element -->
<xsl:template match="book">
<div class="book-item">
<xsl:value-of select="title" />
</div>
</xsl:template>The name
Attribute: Named Subroutines
The name attribute assigns an explicit identifier to a
template, effectively turning it into a reusable function or subroutine.
A named template is not triggered automatically by the structure of the
incoming XML; instead, it must be explicitly invoked by its name.
- Invocation: Explicitly triggered via
<xsl:call-template name="templateName" />. - Execution Style: Procedural (Pull Model).
- Context Node: The context node remains unchanged from the point of invocation unless parameters are explicitly passed.
- Primary Use Case: Reusing shared formatting logic, utility functions, or recursive loops that do not map directly to a single node pattern.
<!-- Named template acts like a reusable helper -->
<xsl:template name="renderHeader">
<xsl:param name="headerText" />
<h1><xsl:value-of select="$headerText" /></h1>
</xsl:template>
<!-- Calling the named template -->
<xsl:call-template name="renderHeader">
<xsl:with-param name="headerText" select="'Book Catalog'" />
</xsl:call-template>Key Differences at a Glance
| Feature | match Attribute |
name Attribute |
|---|---|---|
| Trigger Mechanism | <xsl:apply-templates> |
<xsl:call-template> |
| Selection Logic | Pattern matching against XML nodes | Direct call by string identifier |
| Context Node | Switches to the matched node | Stays the same as the caller’s context |
| Polymorphism | Supported via template priority and modes | Not supported; calls are static |
| Design Pattern | Declarative / Rule-driven | Procedural / Functional |
Combining match and
name
An <xsl:template> can contain both a
match and a name attribute simultaneously.
This allows the template to be invoked automatically when matching nodes
are processed with <xsl:apply-templates>, while also
permitting explicit calls via <xsl:call-template>
from elsewhere in the stylesheet.