Skip to content
xsl:apply-templates

xsl:apply-templates

Selects a sequence of nodes and processes each one by finding the best-matching template rule.

<xsl:apply-templates select="chapter"/>

This is the central dispatch mechanism of XSLT. For each selected node the processor looks up template rules in the active mode, picks the rule with the highest precedence and priority, and runs its body. If select is omitted, the default is child::node(), so a template that just contains <xsl:apply-templates/> recursively walks the children of the current node.

xsl:apply-templates accepts xsl:sort children to control the order in which selected nodes are processed, and xsl:with-param children to pass parameters that match templates can declare with xsl:param.

Attributes

Attribute Description
select XPath expression selecting the nodes to process. Default: child::node().
mode Mode in which to look up matching templates. Use #default for the unnamed mode or #current to keep the current mode.

Examples

Sorted apply-templates with a parameter:

<xsl:apply-templates select="book">
  <xsl:sort select="@year" data-type="number" order="descending"/>
  <xsl:with-param name="indent" select="2"/>
</xsl:apply-templates>

Identity transform — copies every node, recursively:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

Switching modes for a different rendering of the same nodes:

<xsl:template match="/">
  <toc>    <xsl:apply-templates select="//section" mode="toc"/></toc>
  <body>   <xsl:apply-templates select="//section"/></body>
</xsl:template>