Precedence for Rules

Before we move on and examine some of the more advanced features of XSL, we need to look at one more topic—precedence. Precedence is a simple idea, really, that defines the order in which we will apply a rule, given that two rules match an input. For example, the following two rules each match Listing elements.

<xsl:template match ='Listing'...</xsl:template>

and

<xsl:template match ='//Listing'...</xsl:template>

Which would win if we were processing an XSL stylesheet? What if one of the rules were from an imported stylesheet? Would that matter? In this section we will find out.

xsl:import and Import Precedence

As we discussed at the very beginning of this chapter, XSLT supports importing other stylesheets via xsl:import. We import another stylesheet as shown in the following:

<?xml version="1.0"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
    indent-result="no" default-space="strip">
<xsl:import href="another stylesheet.xsl"/>
						<xsl:import href="another stylesheet2.xsl"/>
						. . .
						<xsl:import href="another stylesheetn.xsl"/>

<!-Other XSLT statements -->
</xsl:stylesheet>

We can import any number of stylesheets that result in what is called an import tree. If we assume for a moment that we have a stylesheet called main.xsl that imports several other spreads, 1.xsl, 2.xsl, and 3.xsl—with 1.xsl also importing a.xsl and b.xsl, and 3.xsl importing c.xsl—we generate an import tree that looks like Figure 5.4.

Figure 5.4. An import tree.


Precedence is then applied based on post-order traversal of the import tree. Effectively, we can imagine that rules that we have encountered more recently have higher precedence than those encountered in the past. We can see that rules in main.xsl have been encountered most recently because xsl:imports must be the first child element of our stylesheet, so they are of highest precedence. In reverse order, we encounter 3.xsl, which imported c.xsl, resulting in rules in c.xsl being of higher precedence than rules in 3.xsl and likewise backwards through the tree. If you are having trouble with this concept, work through Figure 5.4 a few times and it should help. Remember, most recently encountered equals highest precedence, least recently encountered equals lowest precedence. Figure 5.5 shows the import tree from the bottom up and helps with understanding import order.

Figure 5.5. An import tree from the bottom up.


But what about multiple rules at the same level in the import tree? How are they ordered? In general, the more complex a rule, the lower its priority. Section 5.5 of the XSLT specification gives the following rules for determining priority:

  1. Ignore all rules with a lower precedence as reported by the import tree.

  2. For all other rules:

    1. Treat rules or'd together as multiple rules all of priority 0.

    2. If the rule is a simple name or @simple name, its priority is 0.

    3. If the rule pattern is an NCName followed by a colon followed by a simple name (i.e., foo:name), its priority is -0.25.

    4. If the rule contains a node test (i.e., comment(), text(), processing-instruction(), and so on), its priority is -0.5.

    5. Otherwise the rule's priority is 0.5.

If we look back at our original example, we see that the match pattern "Listing" is simpler than the match pattern "//Listing", so the first is matched and the second is ignored.

Including Stylesheets with xsl:include

In addition to xsl:import, we can also include other XSL documents. The primary difference between import and include is that included stylesheets act as if there were part of the stylesheet in which they were included. That is, they do not create a new node in the import tree. The following XSL snippet shows a sample of how we could include a stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
    indent-result="no" default-space="strip">
<xsl:include href="another stylesheet.xsl"/>
<!-Other XSLT statements -->
</xsl:stylesheet>

Explicit Priority

There is one exception to the previous rule and that is that we can state a priority specifically. We could have made the second rule override the first if we had specified its priority specifically as

<xsl:template match ='//Listing'priority='1.0'>...</xsl:template>

This specifically tells the XSLT processor to use this rule first and then any others.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.152.99