- Updated on 12 May 2016
- 2 minutes to read
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.
We begin in the Designer Folder and select Create Flow .
Next, we Name the Flow and click CREATE to proceed to the Flow Designer.
In the Flow Designer we add Create Data step from the Favorite Steps category.
Then, in the resulting window we select Show Editor underData Definitions .
We Name it and define as a String Type. Click CLOSE to continue.
Next we use Text Merge.Plain Mapping type for our data definition and click Show Editor.
In theText Merge Editor we past our XML and click OK to save and close Text Merge Editor.
Then, we add XPath Query step from the All Steps [Catalog] > Data > XML category.
Select XPath Query step on the workspace to set it up. First check Get Xpath from Input checkbox.
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 . This query selects the first book element that is the child of the bookstore element.
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 .
And, the outcome from the XPath Query step is first book in our bookstore.
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