XPath Abbreviations Quick Reference

Table 104 defines the abbreviations you can use in XPath expressions:

Abbreviation
Description
No axis is specified in a location step.

The child axis is assumed. For example, the following two XPath expressions both return the para children of chapter children of the context node:

chapter/para 
child::chapter/child::para 
@

The attribute axis. For example, the following two XPath expressions both return para children of the context node that have type attributes with a value of warning:

para[@type="warning"] 
child::para[attribute::type="warning"] 
//
The descendant-or-self axis. For example, the following two XPath expressions both return all para descendants of the context node:
//para 
/descendant-or-self::node()/child::para 
However, it is important to note that the following two expressions are not equivalent:
The first expression selects the first para element that is a descendant of the context node. The second expression selects each para descendant that is the first para child of its parent.
.
A single dot is the abbreviation for self::node(). This selects the context node. For example, the following two XPath expressions both return all para descendants of the context node:
.//para 
self::node()/descendant-or-self::node()/child::para 
..
A double dot is the abbreviation for parent::node(). This selects the parent of the context node. For example, the following two XPath expressions both return the title children of the parent of the context node:
../title 
parent::node()/child::title 
Table 104. XPath Abbreviations Quick Reference

Table 105 shows examples of abbreviations in XPath expressions

Example
Description
para
Selects the para children of the context node
*
Selects all element children of the context node
node_test
Evaluates all children of the context node and returns those that test true for the particular node_test
*/para
Selects all para grandchildren 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
/doc/chapter[5]/section[2]
Selects the second section of the fifth chapter of the doc child of the context node
para[@type="warning"]
Selects para children of the context node that have type attributes with a value of warning
para[@type="warning"][5]
Selects the fifth para child of the context node that has a type attribute with a value of warning
para[5][@type="warning"]
Selects the fifth para child of the context node if that child has a type attribute with a value of warning
chapter[title]
Selects the chapter children of the context node that have one or more title children
//para
Selects all para descendants of the document root
chapter//para
Selects all para descendants of chapter children of the context node
//olist/item
Selects all item elements that have olist parents
.
Selects the context node
.//para
Selects the para descendants of the context node
..
Selects the parent of the context node
@*
Selects all attributes of the context node
@name
Selects the name attribute of the context node
../@name
Selects the name attribute of the parent of the context node
Table 105. Abbreviations in XPath Expressions

 
Free Stylus Studio XML Training: