XPath in Quantum Fabric

XPath (XML Path Language) is a query language for selecting nodes from an XML document. Quantum Fabric supports XPath expressions to compute/filter (for example, strings, numbers, or Boolean values) a back-end response that is in XML format. Quantum supports XPath for XML, SOAP, and JSON integration services.

For more information about XPath, refer https://www.w3schools.com/xml/xml_xpath.asp.

XPath supported operators in Quantum Fabric

The following table details the supported operators for XPath expressions in Quantum Fabric.

Operators Description
AND Boolean and
OR Boolean or
+ Plus
- Minus
* Multiply
Div Division
Mod Modulus (division remainder)
Sum This converts the value of each node in the node-set to a number and totals the result.
Round This returns the closest integer to the argument. The rounding rules follow Java conventions which are not quite the same as the XSL rules.

 

  • While configuring XPath for a response, for arithmetic XPath expressions, operators and operands should be separated by at least one space ( ).

    For example,

     <? xml version="1.0" encoding="UTF-8”?>
    <CATALOG> <PLANT>
    <ZONE>4</ZONE>
    <PRICE>2.44</PRICE> </PLANT>
    </CATALOG>
    *Sample arithmetic operations with at least one space:*
    //CATALOG/PLANT/ZONE + //CATALOG/PLANT/PRICE //CATALOG/PLANT/ZONE//CATALOG/PLANT/PRICE //CATALOG/PLANT/ZONE div //CATALOG/PLANT/PRICE

How to use XPath in Quantum Fabric

  1. Create an integration service of an app for XML, SOAP, or JSON.
  2. Create an operation.
  3. Configure the input parameters in the Request Input.

    For example:

  4. Select the environment from the Select an Environment list.
  5. Click Save and Fetch Response to view the results of the operation.

    The back-end response is displayed in the XML format, in the Test Result section.
    In Backend Response window, you can view the raw response as either raw data or in a tree format.

    Clicking on an element in the tree to display the XPath of that tag.

  6. Now apply XPath expressions for extracting the required elements from the back-end response of the service call.

    For example, the following is a sample XPath expressions

    Name/ID XPath Scope Data Type Collection ID Record ID
    articles //channel response Collection    
    articles2 //channel response Record articles  
    title item/title response Sting string   articles2
    url item/media:group/media:content/@url response Sting string   articles2
    • Collection - A group of data, also referred to as data set. A collection contains only records, and a record contains a string, boolean, or number values.
    • Record - A group data elements under the specified parameter. A record can also be part of a collection. Typically, a record provides metadata to a segment.

    NOTE: For JSON integration service, the back-end response will be in JSON format. JSONPath format should contain $ in the expression.

    For example: $ the root object/element

  7. Click Save and Fetch Response again.

    Now the back-end response is filtered based on the XPath expressions and displays the output in JSON format.

XPath Example Reference

The following example details a sample backend response, and XPath configurations and Output Result based on the XPath configurations

  • The following is a sample Backend Response, in XML, from the Target URL of an integration service.
    Sample Target URL link: http://www.mocky.io/v2/5a9fa4e92e0000630074d133
  • The following table details the XPath for the AND operator and Output Result based on the sample response.
    XPath for AND operator Output Result
    computerBooks: book [genre = 'Computer' and author = "O'Brien, Tim"]
    {
      "httpStatusCode": 200,
      "catalog": [
        {
          "computerBooks": {
            "computerAuthor": "O'Brien, Tim",
            "computerTitle": "Microsoft .NET: The Programming Bible"
          }
        },
        {
          "computerBooks": {
            "computerAuthor": "O'Brien, Tim",
            "computerTitle": "MSXML3: A Comprehensive Guide"
          }
        }
      ],
      "opstatus": 0
    }
  • The following table details the XPath for the OR operator and Output Result based on the sample response.
    XPath for OR operator Output Result
     fictionBooks: book [genre = 'Fantasy' or genre = 'Science Fiction']
     {
      "httpStatusCode": 200,
      "catalog": [
        {
          "fictionBooks": {
            "fictionTitle": "Midnight Rain",
            "fictionGenre": "Fantasy"
          }
        },
        {
          "fictionBooks": {
            "fictionTitle": "Maeve Ascendant",
            "fictionGenre": "Fantasy"
          }
        },
        {
          "fictionBooks": {
            "fictionTitle": "Oberon's Legacy",
            "fictionGenre": "Fantasy"
          }
        },
        {
          "fictionBooks": {
            "fictionTitle": "The Sundered Grail",
            "fictionGenre": "Fantasy"
          }
        },
        {
          "fictionBooks": {
            "fictionTitle": "Paradox Lost",
            "fictionGenre": "Science Fiction"
          }
        }
      ],
      "opstatus": 0
    }
  • The following table details the XPaths for arithmetic operators and Output Result based on the sample response.
    XPath Output Result
    • add: count(//book[genre = 'Fantasy']) + count(//book[genre = 'Romance'])
    • romanceCount: count(//book[genre = 'Romance'])

    • fantasyCount: count(//book[genre = 'Fantasy'])

    • computerAuthorCountcount(//book[genre = 'Computer' and author = "O'Brien, Tim"])
    • fictionCount: count(//book[genre = 'Fantasy' or genre = 'Science Fiction'])
    • subtract: count(//book[genre = 'Fantasy']) - count(//book[genre = 'Romance'])
    • divide: count(//book[genre = 'Fantasy' or genre = 'Science Fiction']) div count(//book[genre = 'Romance'])
    • multiply: count(//book[genre = 'Fantasy']) * count(//book[genre = 'Romance'])
    • modulus: count(//book[genre = 'Fantasy' or genre = 'Science Fiction']) mod count(//book[genre = 'Romance'])
    {
      "add": 6,  
     "romanceCount": 2, "fantasyCount": 4,    
      "computerAuthorCount": 2,
    "
    fictionCount": 5,
    "subtract": 2,
    "divide": 2.5,
    "multiply": 8, "modulus": "1", "httpStatusCode": 200 }