Next page Previous page Start of chapter End of chapter

XPath abbreviated syntax

Some axes are used more than other. For them, the following shortcuts are available:

For instance, the query:

/descendant::a/child::b[attribute::c]/parent::*[self::* = "w"]

may be written as follows:

//a/b[@c]/..[. = "w"]

Notice that the query //a[//b] expands to /descendant::a[/descendant::b]. The latter selects all root descendants labelled with a if there exists a root descendant labelled with b, and it returns the empty set otherwise. In particular, it is not equivalent to /descendant::a[descendant::b], which selects all root descendants labelled with a having a descendant labelled with b. There exists no abbreviation for descendant::b.

You can try all the above queries with any XPath processor. An XPath processor is a software that evaluates XPath queries. BaseX is a complete Java-based XPath processor with a nice graphic web interface. It shows results in different formats including text, tree and tree map. Saxon is an XSLT and XQuery command line processor. Since XPath is both used in XSLT and XQuery, you can try XPath queries with Saxon as well. For instance, to evaluate the query /descendant::article on the XML document biblio.xml, run the following command:

java net.sf.saxon.Query -s biblio.xml "{/descendant::article}"

The option -s sets the initial context node to the root of the given XML document. If you prefer to store the query in the file articles.xpl, then you can type the following command:

java net.sf.saxon.Query -s biblio.xml articles.xpl
Next page Previous page Start of chapter End of chapter
Caff�XML - Massimo Franceschet