2.5 Abbreviated Syntax
Abbreviated Syntax
Here are some examples of location paths using abbreviated
syntax:
-
para selects the para element children of
the context node
-
* selects all element children of the
context node
-
text() selects all text node children of the
context node
-
@name selects the name attribute of
the context node
-
@* selects all the attributes of the
context node
-
para[1] selects the first para child of
the context node
-
para[last()] selects the last para child
of the context node
-
*/para selects all para grandchildren of
the context node
-
/doc/chapter[5]/section[2] selects the second
section of the fifth chapter of the
doc
-
chapter//para selects the para element
descendants of the chapter element children of the
context node
-
//para selects all the para descendants of
the document root and thus selects all para elements in the
same document as the context node
-
//olist/item selects all the item
elements in the same document as the context node that have an
olist parent
-
. selects the context node
-
.//para selects the para element
descendants of the context node
-
.. selects the parent of the context node
-
../@lang selects the lang attribute
of the parent of the context node
-
para[@type="warning"] selects all para
children of the context node that have a type attribute with
value warning
-
para[@type="warning"][5] selects the fifth
para child of the context node that has a type
attribute with value warning
-
para[5][@type="warning"] selects the fifth
para child of the context node if that child has a
type attribute with value warning
-
chapter[title="Introduction"] selects the
chapter children of the context node that have one or
more title children with
[string-value]
equal to
Introduction
-
chapter[title] selects the chapter
children of the context node that have one or more title
children
-
employee[@secretary and @assistant] selects all
the employee children of the context node that have both a
secretary attribute and an assistant
attribute
The most important abbreviation is that child:: can be
omitted from a location step. In effect, child is the
default axis. For example, a location path div/para is
short for child::div/child::para.
There is also an abbreviation for attributes:
attribute:: can be abbreviated to @. For
example, a location path para[@type="warning"] is short
for child::para[attribute::type="warning"] and so selects
para children with a type attribute with
value equal to warning.
// is short for
/descendant-or-self::node()/. For example,
//para is short for
/descendant-or-self::node()/child::para and so will
select any para element in the document (even a
para element that is a document element will be selected
by //para since the document element node is a child of
the root node); div//para is short for
div/descendant-or-self::node()/child::para and so
will select all para descendants of div
children.
NOTE:
The location path //para[1] does
not mean the same as the location path
/descendant::para[1]. The latter selects the first
descendant para element; the former selects all descendant
para elements that are the first para
children of their parents.
A location step of . is short for
self::node(). This is particularly useful in
conjunction with //. For example, the location path
.//para is short for
self::node()/descendant-or-self::node()/child::para
and so will select all para descendant elements of the
context node.
Similarly, a location step of .. is short for
parent::node(). For example, ../title is
short for parent::node()/child::title and so will
select the title children of the parent of the context
node.
Abbreviations
|