executeXpath
Table of Contents
Overview
The executeXpath() function takes XML/JSON formatted text and XPath as arguments, and returns the values of the desired node from the text.
Return Type
- TEXT
Syntax
<variable> = <xml/json>.executeXPath(<xpath>);
Parameter | Data type | Description |
<variable> | TEXT | Variable which will contain the returned response. |
<xml/json> | TEXT | The XML/JSON formatted text from which the values of the selected node will be returned. Note: When using a json object, we have to add an additional '/root' node in the script, and the return value is embedded within tags represented by the specified key. This is illustrated in the examples section on this page. |
<xpath> | TEXT | The path to select the required nodes in the XML/JSON formatted text. An invalid Xpath will return a runtime error. |
Example 1: Use XPath to fetch value from XML file
xmlResp = invokeurl [ url : "https://www.w3schools.com/xml/cd_catalog.xml" type : GET ] ; info xmlResp.executexpath("/CATALOG/CD/TITLE");
Example 2: Use XPath to fetch value from JSON file
jsonResp = invokeurl [ url : "https://tools.learningcontainer.com/sample-json.json" type : GET ] ; info jsonResp.executexpath("/root/address/city"); // returns <city>Surat</city>
Example 3: Use XPath to fetch attribute value
text = "<a href=\"https://eg4.nic.in/jnv/DFILES/EBOOKS/IR/HarryPotter.pdf\" target=\"_blank\">Harry Potter</a>"; info text.executeXPath("/a/@href").executeXpath("/href/text()"); // returns https://eg4.nic.in/jnv/DFILES/EBOOKS/IR/HarryPotter.pdf
Example 4: Use XPath to fetch specific node
xmlString = "<bookstore><book category=\"COOKING\"><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category=\"CHILDREN\"><title lang=\"en\">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book category=\"WEB\"><title lang=\"en\">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book></bookstore>"; info xmlString.executeXPath("/bookstore/book[3]").executeXPath("/book/title/text()"); //Returns the value within the <title> tag of the 3rd node // returns Learning XML