XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (489 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
8.61Mb size Format: txt, pdf, ePub

If you write a stylesheet with a lot of template rules, then the time taken to find the particular rule to apply to a given node can make a significant difference. The exact way in which different XSLT processors do the matching may vary, but one thing you can be sure of is that patterns containing complex predicates will add to the cost.

Conflict Resolution

When a pattern is used in the definition of a template rule, it is possible that several patterns may match the same node. There are rules for resolving this conflict, which are described in the section
Choosing a Template Rule
in the entry for

in Chapter 6, page 242. One of the factors these rules take into account is the
default priority
of the pattern, which is determined from the way it is written.

The default priority is decided according to the following rules. A numerically higher value indicates a higher priority. Note that some of the priorities are fractional, and some are negative.

If the pattern is a union of two or more patterns (
P1|P2
), then the processor treats it as if there were two completely separate template rules specified, one for
P1
and one for
P2
, and it calculates the default priority of
P1
and
P2
independently, using the rules in the table.

If the pattern starts with an axis specifier (
child::
,
attribute::
or
@
)
, this does not affect the priority.

Pattern Syntax
Default Priority
document-node(schema-element(QName))
document-node(element(QName, QName))
schema-element(QName)
element(QName, QName)
schema-attribute(QName)
attribute(QName, QName)
+0.25
QName
document-node(element(QName))
document-node(element(*, QName))
0.0
element(QName)
element(*, QName)
attribute(QName)
attribute(*, QName)
processing-instruction(Literal)
processing-instruction(NCName)
NCName:*
*:NCName
–0.25
/
document-node()
document-node(element())
document-node(element(*))
* element()
element(*)
attribute()
attribute(*)
text()
comment()
processing-instruction()
node()
−0.5
Otherwise
+0.5

These default priorities are carefully chosen to reflect the selectivity of the pattern:

  • The patterns
    node()
    and
    text()
    and
    *
    are not very selective at all; they match any node of the right node kind, so they have a low priority of -0.5.
  • Patterns of the form
    abc:*
    or
    @xyz.*
    are more selective; they will match element or attribute nodes belonging to a particular namespace only, so they have a higher priority than the previous category. Patterns such as
    *:abc
    are also placed in this category, because they are less specific than a pattern that fully specifies the node name but more specific than one that specifies only the kind of node.
  • Patterns such as
    title
    or
    @isbn
    are the ones most commonly encountered; their default priority of 0.0 reflects the fact that in terms of selectivity, they are typical. Other patterns that specify the kind of node as well as its name fall into the same group. Patterns that specify the schema type of the node without specifying its name; for example,
    element(*, pers:employee)
    are placed at the same level because they might be more or less specific than a pattern specifying the node name.

Other books

Finding Home by Ann Vaughn
House of Mercy by Erin Healy
Captive Pride by Bobbi Smith
An Italian Wife by Ann Hood
Find Me in the Dark by Ashe, Karina
A Gathering of Crows by Brian Keene
ARC: The Buried Life by Carrie Patel