Edit online

Working with XPath Expressions

XPath is a language for addressing specific parts of a document. XPath models an XML document as a tree of nodes. An XPath expression is a mechanism for navigating through and selecting nodes from the document. An XPath expression is, in a way, analogous to an SQL query used to select records from a database.
Note: If an XPath expression is run over a JSON document, it is converted to XML and the XPath is executed over the converted XML document.

There are various types of nodes, including element nodes, attribute nodes, and text nodes. XPath defines a way to compute a string-value for each type of node.

XPath defines a library of standard functions for working with strings, numbers and boolean expressions.

Examples:

  • child::* - Selects all children of the root node.
  • .//name - Selects all <name> elements and descendants of the current node.
  • /catalog/cd[price>10.80] - Selects all the <cd> elements that have a <price> element with a value larger than 10.80.
  • //prolog - Finds all <prolog> elements.
  • //prolog[@platform='mac'] - Finds all <prolog> elements that have the @platform attribute value set to mac.
  • //child::prolog - Selects all @prolog elements and the child content.
  • /*[count(//accountNumber) > 5] - Searches for instances where more than 5 <accountNumber> elements are found.
  • collection('file:/C:/path/to/folder/?select=*.xml')/*[not(//prolog)] - Finds a list of all XML files that do not contain any <prolog> elements.

To find out more about XPath, see http://www.w3.org/TR/xpath.