XPath - Some Important Abbreviations

admin's picture

The most important abbreviation is thatchild::can be omitted from a location step. In effect,child is the default axis.
 
For example, a location path
 
Data/username is short for
child::Data/child::username
There is also an abbreviation for attributes: attribute::can be abbreviated to @.
 
For example, a location path
 
/*/*/*/*/*/*/*/perm[@type="admin"]is short for
/*/*/*/*/*/*/*/child::perm[attribute::type="admin"]
 
... and so selects
 
permchildren with a type attribute with value equal to admin.
//is short for /descendant-or-self::node()/.
 
For example,
 
1.)
 
//perm is short for
/descendant-or-self::node()/child::perm
 
... and so will select anyperm element in the document (even a perm element that is a document element will be selected by //perm since the document element node is a child of the root node);
 
2.)
 
Data//perm is short for
Data/descendant-or-self::node()/child::perm
 
... and so will select all perm descendants of Data children.
 
  • 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
 
  • Example that can be tested on example XML_example.xml:
 
//getUserToken/usertoken/permissions/../usernameis short for
//getUserToken/usertoken/permissions/parent::node()/child::username