Path Expressions
The most straightforward kind of query simply selects elements or attributes from an input document. This type of query is known as a path expression. For example, the path expression:
doc("catalog.xml")/catalog/productwill select all the product elements from the catalog.xml document.
Path expressions are used to traverse an XML tree to select elements and attributes of interest. They are similar to paths used for filenames in many operating systems. They consist of a series of steps, separated by slashes, that traverse the elements and attributes in the XML documents. In this example, there are three steps:
doc("catalog.xml")calls an XQuery function nameddoc, passing it the name of the file to opencatalogselects thecatalogelement, the outermost element of the documentproductselects all theproductchildren ofcatalog
The result of the query will be the four product elements, exactly as they appear (with the same attributes and contents) in the input document. Example 1-4 shows the complete result.
Example 1-4. Four product elements selected from the catalog
<product dept="WMN"> <number>557</number> <name language="en">Fleece Pullover</name> <colorChoices>navy black</colorChoices> </product> <product dept="ACC"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC"> <number>443</number> <name language="en">Deluxe Travel Bag</name> </product> <product dept="MEN"> <number>784</number> <name language="en">Cotton Dress ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access