apply-templates vs call-template: What Is the Difference?
In Extensible Stylesheet Language Transformations (XSLT), both
<xsl:apply-templates> and
<xsl:call-template> are used to invoke templates, but
they operate on fundamentally different execution models. The primary
difference is that <xsl:apply-templates> uses
declarative pattern matching to process nodes dynamically based on the
input XML structure, whereas <xsl:call-template>
operates imperatively, executing a specific named template directly like
a traditional subroutine or function. Understanding how each manages
context, rules, and document flow is critical for writing maintainable,
idiomatic XSLT stylesheets.
Understanding
<xsl:apply-templates>
The <xsl:apply-templates> instruction tells the
XSLT processor to select a set of nodes and find the best-matching
template rule defined with a match attribute.
Key characteristics include:
- Declarative and Data-Driven: The processor evaluates the XML document hierarchy. You do not specify which template will execute; instead, the processor selects the most specific template matching the selected node.
- Context Node Switching: When processing a selected
node set, each node in the set becomes the current context node
(
.) inside the matched template. - Polymorphism: Different node types (elements,
attributes, text) automatically map to their respective matching
templates without conditional
if/elseorchoosestatements. - Modes and Priorities: It supports attributes like
mode(to process the same nodes differently in different passes) andpriority(to resolve conflicts when multiple templates match the same node).
Example:
<!-- Invocation -->
<xsl:apply-templates select="book/chapter" />
<!-- Template Definition -->
<xsl:template match="chapter">
<section>
<xsl:apply-templates select="title | paragraph" />
</section>
</xsl:template>Understanding
<xsl:call-template>
The <xsl:call-template> instruction directly
invokes a template identified by its name attribute,
bypassing pattern matching entirely.
Key characteristics include:
- Procedural and Explicit: It functions identically to a procedure or function call in procedural programming languages. The template executed is hard-coded by name.
- Context Preservation: Calling a named template does not change the current context node or position. The called template executes within the exact same node context as the caller unless explicit parameters are passed.
- Code Reusability: It is primarily used for shared utility logic, string manipulation routines, mathematical helpers, or rendering standardized user interface wrappers.
- Explicit Parameter Passing: Data must typically be
passed explicitly into the called template using
<xsl:with-param>.
Example:
<!-- Invocation -->
<xsl:call-template name="render-header">
<xsl:with-param name="title" select="'Summary Report'" />
</xsl:call-template>
<!-- Template Definition -->
<xsl:template name="render-header">
<xsl:param name="title" />
<header>
<h1><xsl:value-of select="$title" /></h1>
</header>
</xsl:template>Side-by-Side Comparison
| Feature | <xsl:apply-templates> |
<xsl:call-template> |
|---|---|---|
| Programming Paradigm | Declarative / Rule-based | Imperative / Procedural |
| Target Resolution | Resolved dynamically via match attribute |
Resolved explicitly via name attribute |
| Context Node | Changes to the node being processed | Stays the same as the calling instruction |
| Node Set Iteration | Iterates over selected node sets automatically | Does not iterate; executes once per call |
| Coupling | Loosely coupled with input XML structure | Tightly coupled with the target template name |
| Support for Modes | Supported (mode="...") |
Not supported |
When to Use Which Directive
Use <xsl:apply-templates> when transforming
document hierarchies, reordering or filtering elements, and leveraging
recursive descent through arbitrary XML trees. It enables modular
stylesheets that can adapt to structural variations without rewriting
control flow.
Use <xsl:call-template> when creating isolated
helper functions, performing recursive mathematical or string-processing
calculations, or generating fixed structural components that do not
depend on matching specific XML input nodes.