I'm trying to learn XPath query using command line tools in Linux (I'm taking Stanford's Class2Go course Introduction to Databases). Given an XML file called BookstoreQ.xml of a Bookstore that contains both Books and Magazines, I can run the following query at the command line:
$ java -cp Saxon-HE-9.4.0.6.jar net.sf.saxon.Query -s:"BookstoreQ.xml" \
-qs:'<results>{/Bookstore/(Book|Magazine)/Title}</results>'
and it will return the following result:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<Title>A First Course in Database Systems</Title>
<Title>Database Systems: The Complete Book</Title>
<Title>Hector and Jeff's Database Hints</Title>
<Title>Jennifer's Economical Database Hints</Title>
<Title>National Geographic</Title>
<Title>National Geographic</Title>
<Title>Newsweek</Title>
<Title>Hector and Jeff's Database Hints</Title>
</results>
I get the same results if I use xmllint at the command line, as follows:
$ xmllint --xpath '/Bookstore/Book/Title | /Bookstore/Magazine/Title'
However, if I try to use the same exact XPath query as in the Saxon example, then I get an error as follows:
$ xmllint --xpath '/Bookstore/(Book|Magazine)/Title' BookstoreQ.xml
XPath error: Invalid Expression
/Bookstore/(Book|Magazine)/Title
^
xmlXPathEval: evaluation failed
XPath evaluation failure
why?
UPDATE:
Thanks to Francis and Michael for helping me to understand the issue. A workaround for playing with XPath at the command line in linux is to use something similar to the script below.
#!/bin/bash
# This script to run xpath queries
java -cp Saxon-HE-9.4.0.6.jar net.sf.saxon.Query -qs:"<results>{$1}</results>" \
\!indent=yes
echo
It assumes that you've placed saxon somewhere in your Java classpath. Thus, the following query below will output the above results (correctly indented):
$ xpath.sh "doc('BookstoreQ.xml')/Bookstore/(Book|Magazine)/Title"