For more information on the XPath syntax , head over to the official W3C specification .

You can also find an online XPath evaluator here .

Note that XPath expressions have a directory-path-like syntax.

Here are a few tips to help you work with XML payloads:

  • A single / selects from the root node.

  • /list identifies the "list object" at the top level of the XML document. Then you can iterate over attributes and sub attributes. The language natively supports arrays.

  • The slash sign / allows you to get attributes of an object or to go deeper in the tree.

  • The square brackets [] enable to select a specific item in an array by its position. Please note that the index starts from 1.

  • /text() allows you to get a node inner text.

  • // selects nodes in the document from the current node that match the selection no matter where they are.

Sample XPath expressions

  <?xml version="1.0" encoding="UTF-8"?>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>

  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>

  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>

  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>

Here are some sample expressions and their result.

Expression Value
/ The whole document node
/bookstore The "bookstore" node
/bookstore/book/title An array of all "title" nodes of all book nodes under the bookstore element
//title An array of all "title" nodes whatever their position is
//title/@lang An array of all lang attributes of the "title" nodes
/bookstore/book/title/text() An array of all text values of "title" nodes of all "book" nodes under the bookstore element
/bookstore/book[price>35]/title The "title" nodes of all "book" nodes having price greater than 35
//book[last()]/title The "title" node of the last "book" node
//book[position() < 3] The two first "book" nodes
//title[@lang]] An array of all "title" nodes with an attribute "lang"
name(//*[1]) The name of the first element in the document (i.e.: bookstore)
count(//title) The number of all "title" nodes (i.e.: 4)