# xsl:apply-templates

The `xsl:apply-templates` element 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.

The `xsl:apply-templates` element takes an optional attribute, `mode`, which 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.

It also takes an optional attribute, `select`.

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 below 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:

- For sorted processing, one or more child xsl:sort elements 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-param elements may be included. The values of these parameters are available to the called template. If the `xsl:with-param` element specifies `tunnel="yes"`, then the parameter is passed transparently through to templates called at any depth, but it can only be referenced by an `xsl:param` element 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 `xsl:param` element specifies `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 position() and last() functions)

- A set of variables, which initially is those variable defined as parameters

Some examples of the most useful forms of select expression are listed below:

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

The full syntax of select expressions is outlined in XPath Expression Syntax.

Next