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

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

An Informal Definition

Because they are written in terms of expressions, the formal rules for a pattern such as
book//para
, encourage you to think of the pattern as being evaluated from left to right, which means finding a

element and searching for all its

descendants to see if one of them is the one you are looking for.

An alternative way of looking at the meaning of this expression, and the way in which most XSLT processors are likely to implement the pattern-matching algorithm, is to start from the right. The actual logic for testing a node against the pattern
book//para
is likely to be along the lines:

  • Test whether this is a

    element. If not, then it doesn't match.
  • Test whether there is a

    ancestor. If not, then it doesn't match.
  • Otherwise, it matches.

If there are predicates, these can be tested en route, for example to evaluate the pattern
speech [speaker = ’Hamlet’]
, the logic is likely to be the following:

  • Test whether this is a

    element. If not, then it doesn't match.
  • Test whether this element has a

    child whose typed value is
    Hamlet
    If not, then it doesn't match.
  • Otherwise, it matches.

Most patterns can thus be tested by looking only at the node itself and possibly its ancestors, its attributes, and its children. The patterns that are likely to be the most expensive to test are those that involve looking further afield.

For example, consider the pattern
para[last()-1]
, which matches any

element that is the last but one

child of its parent. Most XSLT processors, unless they have an exceptionally good optimizer, are going to test whether a particular

element matches this pattern by counting how many children the parent element has, counting how many preceding

siblings the test

has, and comparing the two numbers. Doing this for every

element that is processed could get a little expensive, especially if there are hundreds of them with the same parent. With the patterns
para[1]
or
para[last()]
you've a slightly better chance that the processor will figure out a quicker way of doing the test, but it would be unwise to rely on it.

Other books

Take It Off by J. Minter
Nausea by Kurtz, Ed
The Christmas Spirit by Susan Buchanan
The Door to Bitterness by Martin Limon
Borrowing Death by Cathy Pegau
Seducing the Ruthless Rogue by Tammy Jo Burns
The Hollywood Guy by Jack Baran
Poached by Stuart Gibbs
Watch Over Me by Daniela Sacerdoti