XPath expressions

XML documents are organized as a tree, consisting of a root node and descendant child nodes. The function library relies on XPath arguments to browse within this tree and locate individual XML nodes.

The result of an XPath expression can be either a node or a set of element, text, or attribute nodes. For example, the XPath expression /ABC/DEF selects all DEF child nodes under the ABC root node of the XML document. The following table summarizes the most common features of XPath syntax.

Table 1. XPath syntax overview
XPath syntax Usage
/ The initial forward slash in an XPath expression specifies the root of the tree. Specify an absolute path with an initial slash. For example, /ABC specifies the ABC child element that is located at the root. If you omit the initial slash, the path is relative, and the context of the relative path defaults to the root element. Use subsequent forward slashes within an XPath expression as path separators to identify the child nodes of any node. For example, /ABC/DEF specifies the DEF element, which is a child of the ABC element, which is a child of the root element.
// Two forward slashes specify all descendants of the current node. For example, ABC//DEF matches any DEF element under the ABC element.
* The asterisk is the wildcard character and specifies a match on any child node. For example, /ABC/*/DEF matches any DEF element that is a grandchild of the ABC element.
[] Brackets specify predicate expressions, such as the binary operators OR, AND, and NOT. For example, /RESIDENTS [AGE=65 and NAME="Jane Doe"]/ADDRESS selects the address element of all residents whose ages are 65 and whose names are Jane Doe. The brackets also denote an index into a list. For example, /POSTOFFICE/BOX[2] identifies the second box number element under the POSTOFFICE root element.
nodename The nodename variable selects all child nodes of the named node. Examples follow:
  • bookstore selects all the child nodes of the bookstore element.
  • /bookstore selects the root element bookstore. If the path starts with a forward slash ( / ), it always represents an absolute path to an element.
  • bookstore/book selects all child book elements of bookstore.
  • book selects all book elements in the document.
  • bookstore//book selects all book elements that are descendents of bookstore, no matter where they are under the bookstore element.
. A single period selects the current node.
.. Two periods select the parent of the current node.
@ The at sign selects attributes. For example, //@lang selects all attributes that are named lang.
func_

name
XPath supports a set of built-in functions such as substring(), round(), and not(). In addition, you can make user-defined functions available by using namespaces.