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

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

root(self::node()) treat as document-node()

This means that it selects the same node as the
root()
function described in Chapter 13, when given the context node
self::node()
as an argument, but raises an error if this node isn't a document node (the
treat as
expression is covered in Chapter 11).

Various errors can arise if you use the
/
expression inappropriately:

  • It's an error if there is no context item. This happens, for example, at the outer level of an XSLT function body.
  • It's an error if there is a context item but the context item isn't a node. This can easily happen in XSLT within the

    instruction, which always sets the context item to a string, or it can happen if you are using the

    instruction to process a result of a function such as
    tokenize()
    or
    distinct-values()
    , which both return a sequence of atomic values.
  • It's an error if the context item is in a tree whose root is something other than a document node. In XPath 1.0, every tree had a document node at its root, in fact, it was called a root node rather than a document node because there was no distinction. But the XPath 2.0 data model allows you to have orphaned trees with no document node. Commonly these will have an element as their root. They can also have other kinds of node as the root; for example, an attribute or text node, but in this case the tree can only contain one node.

The language could have been designed so that
/
was a synonym of the axis step
ancestor-or-self::node()[last()]
, which selects the root of the tree whatever kind of node it is. The designers decided not to do this to avoid the surprises that can otherwise occur if you find yourself at a different kind of node from the one you were expecting. This decision also has the advantage that the type of the expression
/
is known more precisely: it always returns a document node, which means that it is always safe to use it in contexts (such as a call to a user-defined function) where a document node is the required type.

Although I have classified the root expression as an abbreviated axis step, it's unusual to use it explicitly as an operand of the
/
operator. Unusual, but not illegal. You will usually need to put it in parentheses if you try it; for example,
$x/(/)
selects the document node at the root of the tree containing the node
$x
. A unary
/
operator appearing in an expression such as
/book/chap/title
can be seen as an abbreviation for
(/)/book/chap/title
, so the root expression is being used as an implicit axis step whenever you write a path expression starting with
/
. Which forms the subject of the next section… .

Other books

An Improper Holiday by K.A. Mitchell
Whisperings of Magic by Karleen Bradford
You Are a Writer by Jeff Goins, Sarah Mae
The Boston Stranglers by Susan Kelly
Dead No More by L. R. Nicolello
Murder Comes Calling by C. S. Challinor
Rogue's Hollow by Jan Tilley