|
Home > Online Product Documentation > Table of Contents > How the XSLT Processor Applies a Stylesheet How the XSLT Processor Applies a StylesheetWhen 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 Stylesheet used to present this process in more detail in the following topics: Instantiating the First TemplateTo 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
Selecting Source Nodes to Operate OnAside 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
In an
When the XSLT processor reaches an
In an
Controlling the Order of Operation
Typically, the template that matches the root node includes an
1. The processor evaluates the expression specified for the
xsl:apply-templates
select 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
Example
The
sample template on
Instantiating the First Template contains the following
The
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
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
Omitting Source Data from the Result DocumentThe 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 Source XML document includes more than the title, author, and price for each book. It also includes the year of publication:
However, the template that matches the
When More Than One Template Is a Match
Sometimes, more than one template matches the node selected by an
For information on specifying these attributes, see xsl:template and xsl:apply-templates. When No Templates MatchWhen 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. |