XPath is a language allowing you to easily work with your XML documents. It’s already usable via the DOM Find XML element command and with 4D v18 R3, the support of the XPath syntax has been drastically enhanced. If you’re among those who requested features such as using a wildcard in a path, then keep reading!
To use the new syntax with existing applications, you need to check the “Use standard XPath” compatibility setting. Newly created databases use the new syntax by default. You can now use new expressions such as //, @, *, last() to simplify your searches.
Examples of writing predicates
We’ll use the following XML document in the examples below:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book> <title lang="en">The Wheel of Time</title> <title lang="fr">La roue du temps</title> <price>29.99</price> </book> <book> <title lang="en">Harry Potter</title> <price>50.00</price> </book> <book> <title lang="en">Learning XML</title> <price>39.95</price> </book> </bookstore>
- Find the next to last book element that is the child of the bookstore element:
$Dom_root2:=DOM Find XML element($Dom_root;"/bookstore/book[last()-1]";$arrAfound)
// Returns
//‹book›
// ‹title lang="en"›Harry Potter‹/title›
// ‹price›50.00‹/price›
//‹/book›
- Find all the title elements that have a “lang” attribute with a value of “fr”:
$Dom_root2:=DOM Find XML element($Dom_root;"//title[@lang='fr']";$arrAfound)
// Returns ‹title lang="fr"›La roue du temps‹/title›
- Find all the book elements that have a price equal to 39.95:
$Dom_root2:=DOM Find XML element($Dom_root;"book[price=39.95]";$arrAfound)</br />
// Returns
//‹book›
// ‹title lang="en"›Learning XML‹/title›
// ‹price›39.95‹/price›
//‹/book›
These are just a few examples of what you can do with the new expressions. You can mix them in a more or less complex way to find the results you need. Refer to the documentation to see what you can do with these new enhancements in more detail.