Next / Previous / Contents / Shipman's homepage

8.3. xsl:apply-templates: Process a node set with appropriate templates

The purpose of this element is to tell the XSLT processor to find the templates that apply to a given set of elements, and apply them. This happens a lot when you are writing the template to process a certain element, and you want to say “now go and process all the children of this element, using whatever templates apply.” Attributes:


To specify which nodes you want to process, set this attribute's value to an XPath expression that computes a node-set. Each node in the resulting set will be processed. This attribute is optional; the default node-set consists of all the children of the current node.


This attribute is used when you want to process the same input content more than once in more than one way. For example, if you have chapter titles in a document, you'll need to process them twice: once to put the title on the first page of the chapter, and also to build the table of contents.

If you supply a mode attribute with your xsl:apply-template element, it will only apply templates that have the same mode attribute. In the example, you'd write a regular template (with no mode attribute) to process the chapter title as part of the chapter body, and you'd write another template as xsl:template mode="toc"... to process the chapter title in the table of contents.

Here are a couple of examples. The most common usage, this means “process all children using the appropriate templates.”:


And this example processes all map children of the current node:

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