XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / apply-templates
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The instruction xsl:apply-templates selects source-tree nodes for processing from the context node. The resulting node sequence can be sorted before processing continues.
Classification | Instruction |
Function group | Template rules and invocation |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:apply-templates is an instruction and is therefore used only inside sequence constructors or template bodies.
The instruction xsl:apply-templates is usually empty but can contain instructions xsl:sort or xsl:with-param. Text content and literal result elements are forbidden.
Attributes:
Standard attributes apply. Two element-specific attributes, select and mode, are optional.
mode
Value | |
Usage | Optional |
Introduction | XSLT 1.0 |
Another attribute, usable alone or with select, is mode. Without a mode attribute, only default-mode template rules, which also lack a mode attribute, can be invoked.
When mode is set, its value must be a QName or one of two predefined values. With a QName, only rules with a matching mode attribute can activate.
Besides QNames, two predefined values designate target template modes: #current and #default:
mode="#current"
An enclosing template rule’s own mode (current mode) is not automatically retained. Retain it explicitly by its QName or implicitly using value#current.mode="#default" (default)
For completeness, value#defaultspecifies that the sequence should invoke only default-mode rules. This is the same behavior asxsl:apply-templatewithout amodeattribute.
Note: Template modes have three predefined values, #current, #default and #all. However, value #all can only be a template rule’s mode and is unavailable here.
select
Value | xpath-pattern |
Usage | Optional |
Introduction | XSLT 1.0 |
The instruction can use the select attribute to select specific nodes. Its value must be a valid XPath pattern. All matching nodes enter the result sequence.
Without a select attribute, the instruction behaves as if its pattern were select="child::node()", selecting all children of the context node.
Purpose:
The instruction xsl:apply-templates builds a node sequence whose matching template rules are successively activated. When the select attribute is set, the XPath pattern determines the sequence, which can also be empty.
When xsl:apply-templates has no select attribute, it selects all current-node children in document order: text nodes, even pure whitespace nodes, elements, comments and processing instructions.
When xsl:apply-templates has no select attribute and its context is not a node, it returns the empty sequence.
Calling a rule depends on its mode. To call rules outside the default mode, explicitly specify exactly one desired mode. (Note: The call must be unambiguous. Unlike xsl:template, lists are forbidden.) If no rule exists in that mode, a corresponding default template is used. The enclosing rule’s current mode is not automatically passed on by xsl:apply-templates.
An exception is the built-in default template rules, which always apply when no matching rule exists. The current mode is irrelevant here but can be passed to subsequent templates through implicit xsl:apply-templates, called sticky mode. This ensures every node awaiting processing is processed.
Relative and absolute XPath patterns for select:
A relative XPath pattern in the select attribute always refers to the current node. This is usual, but absolute patterns relative to the root are also explicitly allowed.
An overview and explanation of built-in default rules appears under xsl:template.
Examples:
Example 1 – Selecting a node sequence:
The select attribute selects specific children of the context node:
<xsl:apply-templates select="buch"/>
The sequence contains only child elements <buch> of the current node, in document order.
Heterogeneous sequences can also be built with an appropriate pattern:
<xsl:apply-templates select="buch | cd"/>
selects, again in document order, all children <buch> and <cd> of the current node.
Example 2 – Building a sorted node sequence:
When used as a container, the element can contain sort instructions xsl:sort. The sequence selected by xsl:apply-templates is then sorted before further processing.
<xsl:apply-templates select="buch">
<xsl:sort select="autor"/>
</xsl:apply-templates>
Here, a sequence of <buch> elements is sorted by the string values of their <autor> elements before further processing.
Instruction xsl:with-param can also pass values to the instantiated rule, which must contain corresponding xsl:param elements to receive them.
<xsl:apply-templates select="buch">
<xsl:with-param name="farbe" select="red"/>
</xsl:apply-templates>
Here, the processing template receives string "red" to use as a presentation color value.
Example 3 – Restricting processing to child elements:
When processing children, the sequence produced by xsl:apply-templates without a select attribute depends on whether whitespace stripping occurred for the context element. Without it, unwanted whitespace nodes enter the sequence and interfere with counting child elements using position().
It may therefore be better to restrict the sequence to elements, by using a wildcard pattern:
<xsl:apply-templates select="*"/>
As intended, this excludes text nodes, but also comments and processing instructions, should you wish to process those with matching rules.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:apply-templates
select = node-set-expression
mode = token>
<!-- Content: (xsl:sort | xsl:with-param)* -->
</xsl:apply-templates>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:apply-templates
select? = node-sequence-expression
mode? = token>
<!-- Content: (xsl:sort | xsl:with-param)* -->
</xsl:apply-templates>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn