XML Node Path Syntax (XPath)¶
Extensible Markup Language (XML) is a set of rules for encoding documents in a format that is both human-readable and machine-readable. QIE implements XML Path Language (XPath) Version 1.0 for referencing nodes in an XML message.
Node paths in this section can be used with getNode, getAllNodes, and other QIE functions that accept a nodePath parameter. See How Node Paths Resolve for behavior details and the Node Path Lookup Dialog to build and validate them interactively against a sample message.
XPath is a W3C standard and has been widely adopted and implemented for navigating XML documents. Extensive XPath documentation exists online including this XPath Tutorial at w3schools.com. Those who are not familiar with XPath are encouraged to review the tutorial referenced above and/or other online resources to better understand the XPath syntax.
Some of the more common XPath expressions are listed below:
| XPath Expression | Description |
|---|---|
| nodename | Selects all child nodes of the named node |
| / | Selects from the root node (A path that starts with a slash (/) always represents an absolute path to an element) |
| // | Selects nodes in the document from the current node that match the selection no matter where they are |
| @ | Selects attributes |
| * | Matches any element node |
| @* | Matches any attribute node |
The XML message below is used with the XPath examples that follow:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
| XPath Expression | Description |
|---|---|
| bookstore | Returns all the child nodes of the bookstore element |
| /bookstore | Returns the root element bookstore |
| bookstore/book | Returns all book elements that are children of bookstore |
| //book | Returns all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element |
| //@lang | Returns all attributes which are named 'lang' |
| /bookstore/book[1] | Returns the first book element that is the child of the bookstore element |
| /bookstore/book[last()] | Returns the last book element that is the child of the bookstore element |
| /bookstore/book[last()-1] | Returns the 2nd to last book element that is a child of the bookstore element |
| /bookstore/book[position()<3] | Returns the first 2 book elements that are children of the bookstore element |
| //title[@lang] | Returns all title elements which have an attribute named lang |
| //title[@lang='eng'] | Returns all title elements which have an attribute named lang with a value of 'eng' |
| /bookstore/book[price>35.00] | Returns all book elements that are children of bookstore and have a price element with a value greater than 35.00 |
| /bookstore/book[price>35.00]/title | Returns all title elements of the book element of the bookstore element that have a price element with a value greater than 35.00 |
| /bookstore/* | Returns all the child nodes of the bookstore element |
| //* | Returns all the elements in the document |
| //title[@*] | Returns all title elements which have any attributes |
| //book/title | //book/price | Returns all the title AND price elements of all book elements |
| //title | //price | Returns all the title AND price elements in the document |
| /bookstore/book/price[.=29.99] | /bookstore/book/price[.=34.49] | Collects all book price elements equal to 29.99 or 34.49 into an array when using source.getAllNodes(). |