XPath (XML Path Language) is a query language for selecting nodes and values from XML documents. In Apidog, XPath is used to extract data from XML API responses, validate XML structures, and create assertions for XML-based APIs. You can use XPath to select nodes or sets of nodes in an XML document by following a path or series of steps through the document hierarchy. XML Example# We will use this XML document in the following examples: <?xml version="1.0" encoding="UTF-8" ?>
<bookstore >
<book >
<title lang ="eng" > Harry Potter</title >
<price > 29.99</price >
</book >
<book >
<title lang ="eng" > Learning XML</title >
<price > 39.95</price >
</book >
</bookstore > Node Selection# XPath allows you to select nodes or sets of nodes in an XML document by following a path or steps. Below are some of the most common path expressions: Expression Description nodenameSelect all the children of this node /Select from the root node //Select nodes in the document from the current node that matches the selection, regardless of their positions .Select the current node ..Select the parent node of the current node @Select an attribute
Path Expression Examples# The following table shows path expressions and their results based on the example XML: Path Expression Result bookstoreSelect all children of the bookstore element /bookstoreSelect the root element bookstore. If the path starts with a forward slash ( / ), then this path always represents an absolute path to an element bookstore/bookSelect all book elements that are children of the bookstore //bookSelect all book elements regardless of their position in the document bookstore//bookSelect all book elements that are children of bookstore elements, regardless of where they are located under the bookstore //@langSelect all attributes named lang
Absolute vs Relative Paths
Paths starting with / are absolute (from root), while paths without / are relative to the current node.
Predicates# You can use predicates to find a specific node or a node containing a specified value. Predicates are enclosed in square brackets and allow for powerful filtering. Predicate Examples# Path Expression Result /bookstore/book[1]Select the first book element of the bookstore child elements /bookstore/book[last()]Select the last book element of the bookstore child elements /bookstore/book[last()-1]Select the second to the last book element of the bookstore child elements /bookstore/book[position()<3]Select the first two book elements of the bookstore child elements //title[@lang]Select all title elements that have an attribute named lang //title[@lang='eng']Select all title elements that have a lang attribute with the value eng /bookstore/book[price>35.00]Select all book elements of the bookstore with the value of the price element greater than 35.00 /bookstore/book[price>35.00]//titleSelect all the title elements of the book element in the bookstore with the price element greater than 35.00
XPath uses 1-based indexing for predicates, unlike JSONPath which uses 0-based indexing. The first element is [1], not [0].
Selecting Unknown Nodes# The XPath wildcard can be used to select unknown XML elements without knowing their exact names. Wildcard Syntax# Wildcard Description *Match any element node @*Match any attribute node node()Match any type of node
Wildcard Examples# Path Expression Result /bookstore/*Select all child elements of the bookstore element //*Select all elements in the document //title[@*]Select all title elements with attributes
Wildcards are useful when working with dynamic XML structures or when you want to select all elements regardless of their names.
Select Multiple Paths# You can select several paths by using the | operator in a path expression, allowing you to combine multiple selections. Multiple Path Examples# Path Expression Result //book/title | //book/priceSelects all title and price elements of the book element //title | //priceSelect all title and price elements in the document /bookstore/book/title | //priceSelect all the title elements of the book element of the bookstore element and all the price elements in the document
Use Cases in Apidog# XPath is particularly useful in Apidog for: XML Response Validation : Extract specific values from XML API responses to verify correctness
SOAP API Testing : Parse and validate SOAP responses
Dynamic Variables : Store extracted XML values for use in subsequent requests
Test Assertions : Create assertions based on XML response data
Data Extraction : Pull specific fields from complex nested XML structures
While JSON is more common in modern APIs, many legacy systems and SOAP-based services still use XML. XPath is essential for testing these APIs.
References#