xsl:apply-templates

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.

Category: instruction
Content: ( xsl:sort | xsl:with-param )*
Permitted parent elements: any XSLT element whose content model is sequence-constructor; any literal result element

Attributes

select?

expression

Sequence of nodes to be processed. If this attribute is omitted, then all the immediate children of the current node are processed.

mode?

token

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.

separator?

{ string }

New in XSLT 4.0. The value is an attribute value template. If present, a text node is formed from the effective value of the attribute, and this text node is inserted into the result sequence after processing every item in the (sorted) input sequence other than the last.

Saxon availability

Available in XSLT 1.0 and later versions. Available in all Saxon editions. Available for all platforms.

Notes on the Saxon implementation

In XSLT 3.0, the xsl:apply-templates instruction can select atomic items as well as nodes, and the match pattern syntax of xsl:template is extended to allow atomic items 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.

For 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:for-each and xsl:iterate has been dropped since Saxon 9.6, because it was found to cause problems when local variables were used.

The separator attribute is available from Saxon 13 in XSLT 4.0; it is available in Saxon 11 and 12 provided that syntax extensions are enabled.

Details

If the 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 xsl:template.

If the select attribute is included, the result must be a sequence of nodes. All nodes selected by the expression are processed.

The 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 xsl:sort and/or xsl:param elements:

The selected nodes are processed in a particular context. This context includes:

The full syntax of select expressions is outlined in XPath syntax; some examples of the most useful forms of select expression are given in the example below.

Examples

Some examples of the most useful forms of select expression:

Expression

Meaning

XXX

Process all immediate child elements with tag XXX

*

Process all immediate child elements (but not character data within the element)

../TITLE

Process the TITLE children of the parent element

XXX[@AAA]

Process all XXX child elements having an attribute named AAA

@*

Process all attributes of the current element

*/ZZZ

Process all grandchild ZZZ elements

XXX[ZZZ]

Process all child XXX elements that have a child ZZZ

XXX[@WIDTH and not(@WIDTH="20")]

Process all child XXX elements that have a WIDTH attribute whose value is not 20

AUTHOR[1]

Process the first child AUTHOR element

APPENDIX[@NUMBER][last()]

Process the last child APPENDIX element having a NUMBER attribute

APPENDIX[last()][@NUMBER]

Process the last child APPENDIX element provided it has a NUMBER attribute

Links to W3C specifications

XSLT 3.0 Specification

XSLT 4.0 Specification

See also

xsl:call-template

xsl:sort

xsl:template

xsl:with-param