<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html> <head> <this-is section="xsl-elements" page="apply-templates" subpage=""/> <!-- Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: xsl:apply-templates</title> <meta name="coverage" content="Worldwide"/> <meta name="copyright" content="Copyright Saxonica Ltd"/> <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:apply-templates"/> <meta name="robots" content="noindex,nofollow"/> <link rel="stylesheet" href="../saxondocs.css" type="text/css"/> </head> <body class="main"> <h1>xsl:apply-templates</h1> <p>The <code>xsl:apply-templates</code> element causes navigation from the current element, usually but not necessarily to process its children. Each selected node is processed using the best-match <code>xsl:template</code> defined for that node. </p> <p>The <code>xsl:apply-templates</code> element takes an optional attribute, <code>mode</code>, which identifies the processing mode. If this attribute is present, only templates with a matching <code>mode</code> parameter will be considered when searching for the rule to apply to the selected elements.</p> <p>It also takes an optional attribute, <code>select</code>.</p> <p>If the <code>select</code> attribute is <i>omitted</i>, 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 <code>*/text()</code>. Child elements similarly are processed using the appropriate template, selected according to the rules given below under <a class="bodylink" href="../xsl-elements/template.xml">xsl:template</a>.</p> <p>If the <code>select</code> attribute is <i>included</i>, the result must be a sequence of nodes. All nodes selected by the expression are processed.</p> <p>The <code>xsl:apply-templates</code> 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 <code>select</code> expression that is used). However the element may include <code>xsl:sort</code> and/or <code>xsl:param</code> elements:</p> <ul> <li content="para"> <p>For sorted processing, one or more child <a class="bodylink" href="../xsl-elements/sort.xml">xsl:sort</a> 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.</p> </li> <li content="para"> <p>To supply parameters to the called template, one or more <a class="bodylink" href="../xsl-elements/with-param.xml">xsl:with-param</a> elements may be included. The values of these parameters are available to the called template. If the <code>xsl:with-param</code> element specifies <code>tunnel="yes"</code>, then the parameter is passed transparently through to templates called at any depth, but it can only be referenced by an <code>xsl:param</code> element that also specifies <code>tunnel="yes"</code>. If the default value, <code>tunnel="no"</code> is used, then the parameter value is available only in the immediately called template, and only if the <code>xsl:param</code> element specifies <code>tunnel="no"</code> (explicitly or by defaulting the attribute).</p> </li> </ul> <p>The selected nodes are processed in a particular <i>context</i>. This context includes:</p> <ul> <li content="para"> <p>A current node: the node being processed</p> </li> <li content="para"> <p>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)</p> </li> <li content="para"> <p>A set of variables, which initially is those variable defined as parameters</p> </li> </ul> <p>Some examples of the most useful forms of select expression are listed below:</p> <table> <tr> <td content="para"> <p> <b>Expression</b> </p> </td> <td content="para"> <p> <b>Meaning</b> </p> </td> </tr> <tr> <td content="para"> <p>XXX</p> </td> <td content="para"> <p>Process all immediate child elements with tag XXX</p> </td> </tr> <tr> <td content="para"> <p>*</p> </td> <td content="para"> <p>Process all immediate child elements (but not character data within the element)</p> </td> </tr> <tr> <td content="para"> <p>../TITLE</p> </td> <td content="para"> <p>Process the TITLE children of the parent element</p> </td> </tr> <tr> <td content="para"> <p>XXX[@AAA]</p> </td> <td content="para"> <p>Process all XXX child elements having an attribute named AAA</p> </td> </tr> <tr> <td content="para"> <p>@*</p> </td> <td content="para"> <p>Process all attributes of the current element</p> </td> </tr> <tr> <td content="para"> <p>*/ZZZ</p> </td> <td content="para"> <p>Process all grandchild ZZZ elements </p> </td> </tr> <tr> <td content="para"> <p>XXX[ZZZ]</p> </td> <td content="para"> <p>Process all child XXX elements that have a child ZZZ</p> </td> </tr> <tr> <td content="para"> <p>XXX[@WIDTH and not(@width="20")]</p> </td> <td content="para"> <p>Process all child XXX elements that have a WIDTH attribute whose value is not "20"</p> </td> </tr> <tr> <td content="para"> <p>AUTHOR[1]</p> </td> <td content="para"> <p>Process the first child AUTHOR element</p> </td> </tr> <tr> <td content="para"> <p>APPENDIX[@NUMBER][last()]</p> </td> <td content="para"> <p>Process the last child APPENDIX element having a NUMBER attribute</p> </td> </tr> <tr> <td content="para"> <p>APPENDIX[last()][@NUMBER]</p> </td> <td content="para"> <p>Process the last child APPENDIX element provided it has a NUMBER attribute</p> </td> </tr> </table> <p>The full syntax of select expressions is outlined in <a class="bodylink" href="../expressions/intro.xml">XPath Expression Syntax</a>.</p> <p>In XSLT 3.0, the <code>xsl:apply-templates</code> instruction can select atomic values as well as nodes, and the match pattern syntax of <code>xsl:template</code> is extended to allow atomic values as well as nodes to be matched. As of Saxon 9.4, not all the extensions to match pattern syntax are implemented, but some are, including in particular the construct <code>match="~itemtype"</code> which matches any item of a specified type, for example <code>match="~xs:integer[. mod 2 = 0]"</code> matches any even integer.</p> <table width="100%"> <tr> <td> <p align="right"><a class="nav" href="attribute.xml">Next</a></p> </td> </tr> </table> </body> </html>