How the XSLT Processor Applies a Stylesheet

When the XSLT processor applies a stylesheet, it starts by automatically selecting the root node for processing and then searching for a template that matches the root node. The XSLT processor then iterates through the process of instantiating templates, selecting nodes in the source document for processing, and matching patterns, until no more templates need to be instantiated.

This section uses the sample stylesheet on Example of a Stylesheet to present this process in more detail in the following topics:

Instantiating the First Template

To apply a stylesheet, the XSLT processor searches for a template that matches the source document root. The XSLT processor then instantiates the matching template and begins to process it line by line.

The specific processing depends on the contents of the template that matches the root node. The parts of the template include

It is important to understand that the contents of the XML source document do not dictate the order of XSLT processing. The XSLT processor performs only those actions that you specify, and operates on only the source nodes that you select. For example:

This template matches the root node. Consequently, the XSLT processor begins processing by instantiating this template. This means it processes each part of the template in the order in which it appears.

In the preceding example, the XSLT processor first copies the first four lines in the template body directly into the result document. Then it executes the xsl:apply-templates instruction. When execution of that instruction is complete, the XSLT processor continues processing this template with the last line in the template body. After that, processing of this template is complete, and processing of the stylesheet is also complete.

Selecting Source Nodes to Operate On

Aside from the root node, the XSLT processor operates on only those nodes in the source document that are selected as the result of executing an XSLT instruction. In a stylesheet, there are two XSLT instructions that select nodes in the source document for processing:

The value of the select attribute is an XPath expression. To evaluate this expression, the XSLT processor uses the current source node as the initial context node. This is the node for which the instruction that contains the select attribute is being executed. For example, if this instruction is in the template that matches the root node, the root node is the current source node.

In an xsl:apply-templates or xsl:for-each instruction, the XSLT processor uses the select expression you specify plus the current source node to select a set of nodes. By default, the new list of source nodes is processed in document order. However, you can use the xsl:sort instruction to specify that the selected nodes are to be processed in a different order. See xsl:sort.

When the XSLT processor reaches an xsl:apply-templates instruction, the XSLT processor processes each node in the list of selected nodes by searching for its matching template and, if a matching template is found, instantiating it. In other words, the XSLT processor instantiates a template for each node if a matching template is found. The matching template might not be the same template for all selected nodes. If the XSLT processor does not find a matching template, it continues to the next selected node.

In an xsl:for-each instruction, the XSLT processor instantiates the embedded template body once for each node in the list of selected nodes.

Controlling the Order of Operation

Typically, the template that matches the root node includes an xsl:apply-templates instruction. When the XSLT processor executes the xsl:apply-templates instruction, it performs the following steps:

1. The processor evaluates the expression specified for the xsl:apply-templatesselect attribute to create a list of the source nodes identified by the expression.
2. For each node in the list, the XSLT processor instantiates the best matching template. (Template properties such as priority and mode allow multiple templates to match the same node.)
3. The processor returns to the template that contains the xsl:apply-templates instruction and continues processing that template at the next line.

It is important to note that in step 2, the matching template might itself contain one or more xsl:apply-templates instructions. As part of the instantiation of the matching template, the XSLT processor searches for a template that matches the nodes identified by the new xsl:apply-templates instruction. In this way, the XSLT processor can descend many levels to complete processing of the first selected node in the initial xsl:apply-templates instruction. The xsl:apply-templates instruction allows you to access any elements in the source document in any order.

Example

The sample template on Instantiating the First Template contains the following xsl:apply-templates instruction:

<xsl:apply-templates select="/bookstore/book"/>

The select attribute specifies "/bookstore/book" as the expression. This selects the set of book elements in the source document as the nodes you want to process. For each selected node, the XSLT processor performs the following steps:

1. The XSLT processor searches the stylesheet for a template that matches "book".
2. When the XSLT processor finds the template that matches the book element, it instantiates it. The following template matches the book elements selected by the xsl:apply-templates instruction:
3. The XSLT processor creates an HTML table row and executes the xsl:value-of instructions. These instructions insert the values for the matching book's title, author, and price elements into the table.

The XSLT processor repeats this process for each book node. In other words, it instantiates this template three times, once for each book element in the source document.

It is important to note that the XSLT processor does not search for a matching template once and then instantiate that matching template for each selected element. Rather, the XSLT processor performs the search for a matching template for each node selected for processing. For each node selected for processing, the XSLT processor

Another way to control the order of operation is to specify the xsl:if, xsl:choose, and xsl:when instructions. See XSLT Instructions Quick Reference.

Omitting Source Data from the Result Document

The XSLT processor operates on only those nodes that you specify. If a node in your XML source document is never referenced in a stylesheet, the XSLT processor never does anything with it.

For example, the sample source XML document on Example of a Stylesheet includes more than the title, author, and price for each book. It also includes the year of publication:

However, the template that matches the book element does not specify any processing for the published element. Consequently, the published elements do not appear in the result document.

When More Than One Template Is a Match

Sometimes, more than one template matches the node selected by an xsl:apply-templates instruction. In this situation, the XSLT processor chooses the best match. Which match is the best match depends on the template's priority, mode, and order in the stylesheet. Priority, mode, and order are template properties that you can set.

For information on specifying these attributes, see xsl:template and xsl:apply-templates.

When No Templates Match

When the XSLT processor cannot find a template that matches a selected node, it uses built-in templates. Every stylesheet includes built-in templates whether or not you explicitly define them.

The XSLT processor supports these built-in templates:

Although Stylus Studio does not explicitly insert these templates in stylesheets you create with Stylus Studio, they are always present. That is, as specified by the W3C XSLT Recommendation, these templates are always defined, whether or not they are explicitly defined. See Using Stylus Studio Default Templates.

 
Free Stylus Studio XML Training: