xsl:apply-templates vs xsl:call-template in XSLT
In XSLT stylesheet development, xsl:apply-templates and
xsl:call-template are the two primary instructions used to
process content and modularize code. While both execute templates, they
represent two distinct programming paradigms: declarative, node-driven
transformation and imperative, procedural invocation. This article
breaks down the fundamental differences between
xsl:apply-templates and xsl:call-template,
detailing how they handle context nodes, matching rules, polymorphism,
and maintainability.
Core Mechanism and Processing Model
xsl:apply-templates(Declarative / Rule-Based): Directs the XSLT processor to find and execute templates that match the specified source nodes. The stylesheet processor examines the selected nodes and queries the available templates to find the best match based onmatchpatterns, priorities, and import precedence.xsl:call-template(Procedural / Direct Invocation): Directly executes a specific template referenced by its uniquenameattribute, functioning like a standard subroutine, function, or method in traditional programming languages.
Key Differences
1. Invocation Target
xsl:apply-templatesuses aselectattribute (evaluating an XPath expression) to identify a node-set. It does not name the target template; the processor determines which template to run based on the<xsl:template match="...">definitions in the stylesheet.xsl:call-templateuses anameattribute to target a specific<xsl:template name="...">. It bypasses the template matching engine entirely.
2. Context Node and Node-Set Iteration
xsl:apply-templateschanges the current context node. If theselectexpression returns five nodes, the matching template is executed five times—once for each node, with that node serving as the current context (.).xsl:call-templateretains the current context node and position. Calling a named template does not iterate over a node-set or change the context node unless explicit child instructions inside the named template alter it.
3. Polymorphism and Loose Coupling
xsl:apply-templatessupports polymorphism. You can write a single<xsl:apply-templates select="item"/>instruction, and the processor will automatically apply different templates depending on whether<item>has specific attributes, child elements, or data types.xsl:call-templateis tightly coupled. The caller must explicitly know the exact name of the template to invoke, removing dynamic dispatch capabilities.
4. Modes and Priority Handling
xsl:apply-templatessupports themodeattribute, allowing the same node-set to be processed multiple times in different ways (e.g., generating a table of contents vs. body text). It also respects templatepriorityand import rules when resolving conflicts.xsl:call-templatedoes not usemodeorpriority. It executes the single named template directly.
5. Parameter Handling
- Both instructions support passing parameters using
<xsl:with-param>. - With
xsl:apply-templates, the parameters are passed to every matching template executed across the selected node-set. - With
xsl:call-template, parameters are passed directly to the single named template target.
Summary of Differences
| Feature | xsl:apply-templates |
xsl:call-template |
|---|---|---|
| Paradigm | Declarative / Rule-driven | Imperative / Procedural |
| Target Identifier | Evaluates node patterns
(match) |
Explicit template name
(name) |
| Context Node | Changes to each selected node | Stays identical to caller’s context |
| Multiple Executions | Automatically loops over selected nodes | Executes exactly once per call |
| Supports Modes | Yes (mode="...") |
No |
| Coupling | Loose (polymorphic) | Tight (static call) |
When to Use Each
Use
xsl:apply-templates when:
- Transforming XML trees where the document structure varies or is deeply nested.
- Processing child elements or attributes dynamically.
- Generating multiple views of the same data using
mode. - Writing clean, idiomatic, and extensible XSLT code.
Use xsl:call-template
when:
- Reusing common formatting logic or utility functions (e.g., string replacement, date formatting).
- Generating static layout structures (like standard page headers or footers) that do not correspond directly to source XML nodes.
- Implementing recursive loops not bound to an underlying XML node hierarchy.