Xpath Query

Last Updated: 07/26/2016 Introduced in Verision: 2.0

XPath uses path expressions to select nodes or node-sets in an XML 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 with clicking Create Flow on the Folder Actions Panel.

createFlow

Next, we Name the Flow and click Create to proceed to the Flow Designer.

nameFlow

In the Flow Designer we add Create Data step from the Favorite Steps category.

addCreateData

Then, in the resulting window we Add New Data Definition.

addDataefinition

We Name it and define as a String Type. Click Ok to continue.

defineStr

Next we use Text Merge.Plain Mapping type for our data definition and click Show Editor.

textEditorOpen

In the Text Merge Editor we past our XML and click Ok to save and close Text Merge Editor.

xmlTextEdited

Then, we add XPath Query step from the All Steps [Catalog] > Data > XML category.

addXpathStepQ

Select XPath Query step on the workspace to set it up. First check Get Xpath from Input checkbox.

getXPathFromInput

Then, we select Value of our Defined XML Data for the To Query Input to this step.

selectXMLval

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.

firstQuery

Then we connect Done outcome from the XPath Query step to the End Step in the Flow. This completes our Flow design. We click Test Flow link on the Top Panel of the Flow Designer to test our Flow.

testFlowFirst

And, the outcome from the XPath Query step is first book in our bookstore.

firstBookOut

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

Additional Resources