Xpath Query
  • 10 Feb 2021
  • 2 Minutes to read
  • Dark
    Light

Xpath Query

  • Dark
    Light

Article Summary

Overview

XPath uses path expressions to select nodes or node-sets within an XML document. The node is selected by following a path or a set of steps. 

The following tutorial demonstrates how to use the XPath Query step in Decisions as means to query XML documents.



Query Examples

The following chart displays a list of the Query elements used in the document's examples, with an explanation of each one.

Query Examples for this exampleWhat they do
/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]/titleSelects 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
//title//price
/bookstore/book/title//price



Example

  1. Navigate to a Designer Project and select the CREATE FLOW button from the top Action bar. 
    1. Select Flow, provide a Name, and click CREATE.
  2. Add a Create Data step from the FAVORITE STEPS category of the Toolbox tab. 
  3. Navigate to the Create Data step's Properties tab.
    1. Under DATA > Data To Create, click SHOW EDITOR.
  4. Name the DataDefinition "XML", and select String [Text] from the Type dropdown. 
  5. CLOSE out of the Data Definitions window. 
  6. From the Properties tab of the Create Data step, under INPUTS, change the mapping for XML to Merge Plain Text, then click SHOW EDITOR. 
  7. Copy and paste the following XML into the Merge Text Editor, then click SAVE.
    <?xml version="1.0"?>
    <bookstore>
    <book>
     <title lang="en">Harry Potter</title>
     <price>29.99</price>
    </book>
    <book>
     <title lang="ru">Russian Book</title>
     <price>10.95</price>
    </book>
    <book>
     <title>Unknown</title>
     <price>55.95</price>
    </book>
    </bookstore>
  8. Attach an XPath Query step from the DATA > XML category in the Toolbox tab, to the Done path of the Create Data step. 
  9. Navigate to the XPath Query step's Properties tab. Then, under QUERY, check Get XPAth from Input.
  10. Under INPUTS, map XML to To Query; then, Constant map /bookstore/book[1] to XPath.
    This Query selects the first "book" element that is the Child of the "bookstore" element.

  11. Connect the Done path from the XPath Query step to the End step; then, Save the Flow. 

    If desired, the Flow may be closed by click X. 



Debug 

  1. From the Flow Designer, select the Debug link from the top Action bar.
  2. Click the START DEBUGGING button.
  3. View the Output Data for the XPath Querystep.
    The XPath Query Output will display Query the first "book" value in the "bookstore."



Was this article helpful?

What's Next