Xpath Query
  • Updated on 12 May 2016
  • 2 minutes to read
  • Print
  • Dark
    Light

Xpath Query

  • Print
  • Dark
    Light

XPath uses path expressions to select nodes or node-sets in anXML document . The node is selected by following a path or steps.
This tutorial demonstrates how to use XPath Query step in Decisions to be able to query XML documents.
Example:
We begin in the Designer Folder and select Create Flow .2018-01-05_130012-1024x593.png

Next, we Name the Flow and click CREATE to proceed to the Flow Designer.
2018-01-05_130120-1024x615.png

In the Flow Designer we add Create Data step from the Favorite Steps category.
2018-01-05_130157-1024x768.png

Then, in the resulting window we select Show Editor underData Definitions .
2018-01-05_130226-1024x768.png

We Name it and define as a String Type. Click CLOSE to continue.
2018-01-05_130320.png

Next we use Text Merge.Plain Mapping type for our data definition and click Show Editor.
2018-01-05_130410.png

In theText Merge Editor we past our XML and click OK to save and close Text Merge Editor.
2018-01-05_130921-1024x608.png

Then, we add XPath Query step from the All Steps [Catalog] > Data > XML category.
2018-01-05_131011-1024x768.png

Select XPath Query step on the workspace to set it up. First check Get Xpath from Input checkbox.
2018-01-05_131250-1024x539.png

Then, we select the Path of our Defined XML Data under Input > To Query to this step. 
In the XPath input to this step we define Constant query: /bookstore/book[1] . This query selects the first book element that is the child of the bookstore element.
2018-01-05_131446.png

Then we connect Done outcome from the XPath Query step to the End Step in the Flow . This completes our Flow design. We select Debug link on the Top Panel of the Flow Designer to test our Flow .
2018-01-05_131523-1024x368.png

And, the outcome from the XPath Query step is first book in our bookstore.
2018-01-05_132225-1024x538.png

Next, we are going to show several possible query examples for this XML document:

  • /bookstore/book[last()] - Selects the last book element that is the child of the bookstore element
  • /bookstore/book[last()-1] - Selects the last but one book element that is the child of the bookstore element
  • /bookstore/book[position()<3] - Selects the first two book elements that are children of the bookstore element
  • //title[@lang] - Selects all the title elements that have an attribute named lang
  • //title[@lang='en'] - Selects all the title elements that have a "lang" attribute with a value of "en"
  • /bookstore/book[price>35.00] - Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
  • /bookstore/book[price>35.00]/title - Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
  • /bookstore/* - Selects all the child element nodes of the bookstore element
  • //* - Selects all elements in the document
  • //title[@*] - Selects all title elements which have at least one attribute of any kind
  • //book/title | //book/price - Selects all the title AND price elements of all book elements
  • //title | //price - Selects all the title AND price elements in the document
  • /bookstore/book/title | //price - Selects all the title elements of the book element of the bookstore element AND all the price elements in the document
Was this article helpful?