Processing Template Rules
When the XSLT processor begins to process the XSLT tree, the processor looks for the template rule that points to the document root element in the source tree.
At its simplest, the template rule that points to this root element is as follows:
<xsl:template match="/firstelement">
Here the value of the match pattern indicates that this rule applies to the first element (represented by firstelement) of the root node (represented by the slash, /). In the Sample XML File (books.xml), the simplest template rule that points to the document root element is the following:
<xsl:template match="/catalog">
After the XSLT processor locates this initial template rule, the processor evaluates the rule and proceeds as follows:
- If the rule has no conditional statements, it most likely directs the processor to simply transform this document root element. In this case, the processor transforms the root element and then looks for a template rule that matches the next highest element in the source tree. The processor transforms that element, and then looks for the next highest element. This process continues for all the elements in the source tree.
- If the rule contains conditional statements, the processor evaluates each statement and executes the appropriate action. Often, each conditional statement directs the XSLT processor through a series of template rules that are specific to the conditional statement. Essentially, this means that the XSLT processor ignores all template rules associated with other conditional statements.
Controlling Processing
As you might have noticed, the processing of an XSLT file is not procedural like it is in most programming languages. That is, the XSLT processor does not sequentially process each template rule as it appears in the XSLT tree.
Under certain circumstances, however, the sequence in which the style sheet is processed can be determined, and to a limited extent controlled:
- Using Conditional Processing XSLT provides the
<xsl:if>
and <xsl:choose>
elements for testing conditions. By using these elements, an XSLT file can direct the processor through a specific series of templates.
- Importing Other Style Sheets An XSLT file can import other XSLT files. The order in which the corresponding
<xsl:import>
elements appear in the original file determines the import precedence, which determines the order in which the processor applies the other XSLT files.
Handling Template Rule Conflicts
While processing an XSLT tree, the XSLT processor might encounter a conflict that must be resolved:
- If the processor finds multiple template rules that match a given node, the processor needs to decide which of these template rules to use. For more information about how the processor makes this decision, see Processing Multiple Matching Templates Rules.
- If there are no matches, the processor uses one of the built-in template rules to decide how to handle the node. For more information about these built-in template rules, see Understanding the Built-in Template Rules.
See Also
The XSLT Processor