Although location paths are not the most general grammatical
construct in the language (a
[LocationPath]
is a special case of an
[Expr]
), they are the most important construct and
will therefore be described first.
Every location path can be expressed using a straightforward but
rather verbose syntax. There are also a number of syntactic
abbreviations that allow common cases to be expressed concisely. This
section will explain the semantics of location paths using the
unabbreviated syntax. The abbreviated syntax will then be explained
by showing how it expands into the unabbreviated syntax (see Abbreviated Syntax).
-
child::para selects the
para element children of the context node
-
child::* selects all element
children of the context node
-
child::text() selects all text
node children of the context node
-
child::node() selects all the
children of the context node, whatever their node type
-
attribute::name selects the
name attribute of the context node
-
attribute::* selects all the
attributes of the context node
-
descendant::para selects the
para element descendants of the context node
-
ancestor::div selects all div
ancestors of the context node
-
ancestor-or-self::div selects the
div ancestors of the context node and, if the context node is a
div element, the context node as well
-
descendant-or-self::para selects the
para element descendants of the context node and, if the context node is
a para element, the context node as well
-
self::para selects the context node if it is a
para element, and otherwise selects nothing
-
child::chapter/descendant::para
selects the para element descendants of the
chapter element children of the context node
-
child::*/child::para selects
all para grandchildren of the context node
-
/ selects the document root (which is
always the parent of the document element)
-
/descendant::para selects all the
para elements in the same document as the context node
-
/descendant::olist/child::item selects all the
item elements that have an olist parent and
that are in the same document as the context node
-
child::para[position()=1] selects the first
para child of the context node
-
child::para[position()=last()] selects the last
para child of the context node
-
child::para[position()=last()-1] selects
the last but one para child of the context node
-
child::para[position()>1] selects all
the para children of the context node other than the
first para child of the context node
-
following-sibling::chapter[position()=1]
selects the next chapter sibling of the context node
-
preceding-sibling::chapter[position()=1]
selects the previous chapter sibling of the context
node
-
/descendant::figure[position()=42] selects
the forty-second figure element in the
document
-
/child::doc/child::chapter[position()=5]/child::section[position()=2]
selects the second section of the fifth
chapter of the doc document
element
-
child::para[attribute::type="warning"]
selects all para children of the context node that have a
type attribute with value warning
-
child::para[attribute::type='warning'][position()=5]
selects the fifth para child of the context node that has
a type attribute with value
warning
-
child::para[position()=5][attribute::type="warning"]
selects the fifth para child of the context node if that
child has a type attribute with value
warning
-
child::chapter[child::title='Introduction']
selects the chapter children of the context node that
have one or more title children with
[string-value]
equal to
Introduction
-
child::chapter[child::title] selects the
chapter children of the context node that have one or
more title children
-
child::*[self::chapter or self::appendix]
selects the chapter and appendix children of
the context node
-
child::*[self::chapter or
self::appendix][position()=last()] selects the last
chapter or appendix child of the context
node
There are two kinds of location path: relative location paths
and absolute location paths.
A relative location path consists of a sequence of one or more
location steps separated by /. The steps in a relative
location path are composed together from left to right. Each step in
turn selects a set of nodes relative to a context node. An initial
sequence of steps is composed together with a following step as
follows. The initial sequence of steps selects a set of nodes
relative to a context node. Each node in that set is used as a
context node for the following step. The sets of nodes identified by
that step are unioned together. The set of nodes identified by
the composition of the steps is this union. For example,
child::div/child::para selects the
para element children of the div element
children of the context node, or, in other words, the
para element grandchildren that have div
parents.