Causes navigation from the current element, usually but not necessarily to process its children. Each selected node is processed using the best-match
xsl:template defined for that node.
Available in XSLT 1.0 and later versions. Available in all Saxon editions.
- Category: instruction
- Content: (
- Permitted parent elements: any XSLT element whose content model is sequence-constructor; any literal result element
- Sequence of nodes to be processed. If this attribute is omitted, then all the immediate children of the current node are processed.
- Identifies the processing mode. If this attribute is present, only templates with a matching mode parameter will be considered when searching for the rule to apply to the selected elements.
Notes on the Saxon implementation⚓︎
In XSLT 3.0, the
xsl:apply-templates instruction can select atomic values as well as nodes, and the match pattern syntax of
xsl:template is extended to allow atomic values as well as nodes to be matched. See Patterns in XSLT 3.0 for more details. All of the extensions to the syntax of match patterns are implemented since Saxon 9.6.
Earlier drafts of XSLT 3.0 introduced a pattern syntax
~typename: for example
~xs:integer would match any integer. With experience this was found not to be very useful, and it has been dropped. Instead a new pattern syntax
.[expression] was introduced: this matches any item for which the effective boolean value of the expression (evaluated with that item as the context item) is true. For example
.[. gt 0] will match any item (necessarily a number) that is greater than zero, while
.[nilled()] matches any element that is nilled (
~typename syntax was supported in Saxon 9.5, but not since Saxon 9.6.
xsl:apply-templates to be streamable, the W3C rules require that the
select expression must be "striding", which essentially means that it may use the child axis but not the descendant axis (to ensure that selected nodes do not overlap each other). Saxon attempts to be more liberal than this, and allow streaming also when the descendant axis is used (the implementation will buffer output in memory if the selected nodes actually overlap). This extension applies only if Saxon streamability extensions are enabled (on the command line or by configuration properties). The equivalent extension for
xsl:iterate has been dropped since Saxon 9.6, because it was found to cause problems when local variables were used.
select attribute is omitted, apply-templates causes all the immediate children of the current node to be processed: that is, child elements and character content, in the order in which it appears. Character content must be processed by a template whose match pattern will be something like
*/text(). Child elements similarly are processed using the appropriate template, selected according to the rules given under
select attribute is included, the result must be a sequence of nodes. All nodes selected by the expression are processed.
xsl:apply-templates element is usually empty, in which case the selected nodes are processed in the order they are selected (this will usually be document order, but this depends on the
select expression that is used). However the element may include
- For sorted processing, one or more child
xsl:sortelements may be included. These define the sort order to be applied to the selection. The sort keys are listed in major-to-minor order.
- To supply parameters to the called template, one or more
xsl:with-paramelements may be included. The values of these parameters are available to the called template. If the
tunnel="yes", then the parameter is passed transparently through to templates called at any depth, but it can only be referenced by an
xsl:paramelement that also specifies
tunnel="yes". If the default value,
tunnel="no"is used, then the parameter value is available only in the immediately called template, and only if the
tunnel="no"(explicitly or by defaulting the attribute).
The selected nodes are processed in a particular context. This context includes:
- A current node: the node being processed.
- A current node list: the list of nodes being processed, in the order they are processed (this affects the value of the
- A set of variables, which initially is those variables defined as parameters.
The full syntax of select expressions is outlined in XPath Expression Syntax; some examples of the most useful forms of select expression are given in the example below.
Some examples of the most useful forms of select expression:
||Process all immediate child elements with tag
||Process all immediate child elements (but not character data within the element)|
||Process all attributes of the current element|
||Process all grandchild
||Process all child
||Process all child
||Process the first child
||Process the last child
||Process the last child