Path Expressions

Basic Syntax
A All <A> nodes within the current context, ie. relative to the current path.
/A The <A> root node, ie. an absolute path.
//A All <A> nodes in the document, regardless of how deeply nested they are.
. The current context, ie. the current node.
.. The parent context, ie. the parent node.
A/B All <B> nodes that are the direct children of <A> nodes within the current context.
A//B All <B> nodes that are the descendants of <A> nodes within the current context.
A/* All nodes that are the direct children of <A> nodes within the current context.
@A The ‘A’ attribute of the current context/node. (An attribute may be treated as the child of its node.)
@* All attributes of the current context/node.
N:A All <A> child nodes from the N namespace within the current context.
N:* All nodes from the N namespace within the current context.
*:A All <A> nodes from all namespaces within the current context.
A[B] All <A> nodes containing a <B> child node.
A[@B] All <A> nodes containing the attribute B.
A/@B All B attributes in <A> within the current context.
A[3] The third <A> node within the current context.
A[B][3] The third <A> node containing a <B> child within the current context.
(A/B)[3] The third <B> node that is a child of an <A> node within the current context.
A/text()[2] The second text node in each <A> node within the current context.
A|B All <A> and <B> child nodes within the current context.
The square brackets [] at the right can assume a Boolean value, an integer, or a node. A complex Boolean expression joined by and/or can be formed out of the three types of values.
Axes
child::* * matches only element nodes. If node() is used instead, text, comment and processing instruction nodes are matched as well.

Multiple nodes may be returned. For example,
preceding-sibling::node()[2] returns the previous node that is two siblings away. Any derived types of node() can be used in place of node(), eg. child::comment().
descendant::*
descendant-or-self::*
self::*
following::*
following-sibling::*
ancestor::*
ancestor-or-self::*
parent::*
preceding::*
preceding-sibling::*
namespace::*
attribute::*
Complex Filters
A[position()<3] The first two <A> nodes.
A[last()] The last <A> child node.
A[B][C] All <A> nodes containing a <B> node and a <C> node.
A[(B or C) and D] All <A> nodes containing a <D> node, and a <B> node or a <C> node.
A[not(B)] All <A> nodes which do not contain a <B> node.
A[B=”C”] All <A> nodes containing the <B> nodes with the value C.
A[.!=”B”] All <A> nodes with a value that is not “B”.
(B|C) [@at eq A/@at] All <B> nodes and <C> nodes with the attribute @at equals that of <A>.
Examples
<?xml version="1.0"?>
<menu restaurant="Golden Outlet">
<dish id="1">Satay <i>Cooked</i></dish>
<drink id="2">Cola</drink>
<dish id="3">Curry Chicken</dish>
<dish id="4" xmlns="http://example.com">
Sweet and Sour Pork
</dish>
</menu>
  1. .
  2. /.
  3. self::node()
  4. /self::node()
  5. let $p := /. return $p
  6. menu/..
  7. menu!..
  8. root()
  9. root(menu/drink)
(10) doc(‘menu.xml’)
GIVE
<?xml version="1.0"?>
<menu restaurant="Golden Outlet">
<dish id="1">Satay <i>Cooked</i></dish>
<drink id="2">Cola</drink>
<dish id="3">Curry Chicken</dish>
<dish id="4" xmlns="http://example.com">
Sweet and Sour Pork
</dish>
</menu>
  1. menu
  2. /menu
  3. menu[1]
  4. menu[@restaurant]
  5. menu[@restaurant=”Golden Outlet”]
  6. menu[drink]
  7. menu[drink=”Cola”]
  8. menu[drink][dish]
  9. child::node()
(10) node()
(11) menu[last()]|menu[position()=1]|menu[1]
(12) if (//dish) then menu else //dish
GIVE
<menu restaurant="Golden Outlet">
<dish id="1">Satay <i>Cooked</i></dish>
<drink id="2">Cola</drink>
<dish id="3">Curry Chicken</dish>
<dish id="4" xmlns="http://example.com">
Sweet and Sour Pork
</dish>
</menu>
  1. /menu/@restaurant
  2. menu//@restaurant
  3. //@restaurant
  4. (//@*)[1]
  5. (//attribute::node())[1]
  6. //attribute(restaurant)
GIVE
restaurant="Golden Outlet"
  1. //dish
  2. menu/dish
  3. //dish[1] | //dish[2]
  4. //dish[1] union //dish[2]
  5. //dish[position()=(1,2)] intersect //dish
  6. /menu/child::node()[@id] except /menu/child::node()[@id=2 or @id=4]
  7. /menu/dish[@id]
  8. /menu/dish[@id<10]
  9. /menu/dish[@id=1 or @id=3]
(10)/menu/dish[@id=1]|//dish[@id=3]
(11)menu/child::node()[@id!=2 and not(@id=4)]
(12)menu/dish[i]|menu/dish[.="Curry Chicken"]
(13)/menu/element(dish)
GIVE
(1) <dish id="1">Satay <i>Cooked</i></dish>
(2) <dish id="3">Curry Chicken</dish>
  1. //i/text()
  2. (//text()[1])[3]
  3. /menu/dish/*/text()
  4. /menu[1]/dish[1]/i[1]/text()
GIVE
Cooked
  1. /menu/dish[2]/following::node()
GIVES
  1. (space)
  2. <dish id="4" xmlns="http://example.com">
  3. Sweet and Sour Pork
  4. (space)
  1. //dish[1] is //dish[@id=1]
  2. //dish[2] >> //dish[@id=1]
  3. //dish[@id=1] << //dish[2]
  4. some $x in //dish satisfies $x[@id>0]
  5. every $x in //dish satisfies $x[@id>0]
  6. count(/menu/child::node()) > count(/menu/child::*)
  7. not(. instance of element())
  8. menu instance of element()
  9. menu instance of element(menu)
GIVE
true
  1. menu/dish/string-length()
  2. ('abcdefghijkl',
    'xmnopqrstuvwx')!string-length()
GIVE
  1. 12
  2. 13