XML Path Syntax
This video tutorial demonstrates, how you may use XPath in vREST:
XPath is used to locate the data in XML object. XPath is currently used in the following two functionalities of vREST:
- Variable Extractor tab
In variable extractor tab of a test case, you need to specify the x-path in "JSON Path / XML PATH / Utility Method" column. - XML Body Assertions
In Assertions tab, you need to specify XPath for XML Body assertions in "Property" column.
Below are some examples of most common scenarios for XML path expressions.
<?xml version="1.0"?> <catalog> <book id="book1"> <author>Author Name 1</author> <title>Book Title 1</title> <genre>Book Genre</genre> <price>50</price> <publish_date>2010-11-01</publish_date> <description>Book Description...</description> </book> <book id="book2"> <author>Author Name 2</author> <title>Book Title 2</title> <genre>Book Genre</genre> <price>15</price> <publish_date>2010-12-13</publish_date> <description>Book Description ...</description> </book> </catalog>
- Accessing Node Attribute (Id of first book)
- XPath Expression:
/catalog/book[1]/@id
- Value: book1
- XPath Expression:
- Accessing Node Text Content (Author of first book)
- XPath Expression:
/catalog/book[1]/author/text()
- Value: Author Name 1
- XPath Expression:
Now, if you are having XML namespaces defined in the XML file as below:
<?xml version="1.0" encoding="utf-8"?> <sample> <hello:table xmlns:table="http://example.com/hello"> <tr> <td>Hello world!!!!</td> </tr> </hello:table> </sample>
In the above XML, to access the td element, you may provide the below XPath expressions:
- /sample/*[local-name()='table' and namespace-uri()='http://example.com/hello']/tr/td/text()
- /sample/*[local-name()='table']/tr/td/text()