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

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

More generally, runtime errors can also occur when evaluating a predicate in a pattern. For example, matching the pattern
//book[@price div 0 = 0]
could cause a runtime error (division by zero). The spec is open ended about these; it makes it clear that whether or not a particular error is ever reported will depend on the order in which the processor chooses to test the different patterns, and even then, on its evaluation strategy for a particular pattern. It also allows processors to ignore such errors, simply behaving as if the pattern does not match.

The other problem that occurs with parentless nodes is that using the definition as we have it so far, the pattern
match = “ex”
means
match = “child::e”
and would match an

element only if it is a child of something. The working group decided that this would be just too confusing, and resolved that the pattern
e
should match every

element whether or not it has a parent. Similarly,
chapter / para
should match every

whose parent is a

, whether or not the

has a parent.

The way that the formal definition of patterns has been bent to meet this requirement is somewhat tortuous. It is done by introducing two new axes,
child-or-top
, and
attribute-or-top
, and using these axes in the first step of a
RelativePathPattern
in place of the usual
child
and
attribute
axes. The
child-or-top
axis selects the children of the context node, unless the context node is a parentless element, text node, comment, or processing instruction, in which case it selects the parentless context node itself. Similarly, the
attribute-or-top
axis selects the context node itself if it is a parentless attribute node. Given these two extra axes, the equivalence between patterns and XPath expressions continues to hold. (These axes, of course, are purely notional. You can't use them explicitly either in an XPath expression or in a pattern.)

Note that this refinement does not apply to patterns starting with
/
. The pattern
//book
, which in XSLT 1.0 matched exactly the same nodes as the pattern
book
, now has a slightly different meaning: it selects only

elements that are descendants of a document node. It will not select a parentless

element, or a

element in a tree whose root is a parentless element. This, incidentally, means that
match = “//book”
is quite likely to be less efficient than
match = “book”
because the system now has to check what kind of node is at the root of the tree.

Other books

Seven Dreams by English, Charlotte E.
Hit & Mrs. by Lesley Crewe
Save the Date by Jenny B. Jones
The Compound by Bodeen, S.A.
Drowning World by Alan Dean Foster
The Wicked Wager by Anya Wylde
The Napoleon of Crime by Ben Macintyre