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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
5.96Mb size Format: txt, pdf, ePub
  • Variants on the
    /
    operator that can be used to write abbreviated path expressions, notably
    /
    as a freestanding expression,
    /
    at the start of a path expression, and the
    //
    pseudo-operator.
  • What I call the Venn operators:
    union
    ,
    intersect
    , and
    except
    . These are often used to combine the results of several path expressions, or to form a step of a path expression.

Examples of Path Expressions

Before describing the different kinds of path expression in more detail, it may be helpful to look at some examples.

Expression
Description
para
Selects all the

element children of the context node.
@title
Selects all the
title
attributes of the context node. The result will either be empty or contain a single attribute node.
book/author/first-name
Selects the

elements that are children of the

elements that are children of the

elements that are children of the context node.
para[@id]
Selects all the

element children of the context node that have an
id
attribute.
para/@id
Selects the
id
attributes of all the

element children of the context node. This differs from the previous example in that the result is a sequence of attribute nodes rather than a sequence of element nodes.
/*/para
Selects all the

element children of the containing document element (that is, of the outermost element of the document containing the context node). The
*
is a wildcard that selects all elements on the chosen axis.
$sections/body
Selects all

element children of nodes in the sequence identified by the variable
$sections
. A type error occurs if
$sections
contains an item that isn't a node. The results will be in document order even if the original sequence
$sections
isn't in document order.
$sections[3]/body
Selects all

element children of the third node in the sequence identified by the variable
$sections
.
$sections/
.
Selects all the nodes that are present in the value of the variable
$sections
, but with duplicates removed, and sorted into document order. The only effect of the
/.
in this case is to force the reordering and deduplication.
/contract/clause[3]/subclause[2]
Selects the second

of the third

of the

that is the document element. If the document element is not a

, or if any of the other components are missing, it produces an empty sequence.
//figure
Selects all the

elements in the document.
city[not(@name = preceding-sibling::city/@name)]
Selects all the child

elements of the context node that do not have a
name
attribute that is the same as the
name
attribute of a preceding

element with the same parent. It thus selects a set of child

elements with unique names.
*/name()
Selects the names of the children of the context node. The path expressions given above all select nodes in a tree. However, the
/
operator can also be used as a simple mapping operator to compute atomic values for each node in a sequence; this example returns a sequence of strings, each being the name of a child element of the context node. I refer to this kind of expression as a
simple mapping expression
, and because this chapter is all about expressions that operate on nodes, I will cover simple mapping expressions in Chapter 10.

Other books

A Magic Broken by Vox Day
Baby Geisha by Trinie Dalton
Tight Lines by William G. Tapply
Hostage by Cheryl Headford
Kings of the North by Elizabeth Moon
Druids by Morgan Llywelyn
Darkness Creeping by Neal Shusterman