XML

Getting to Know Saxon

None of this XQuery stuff would mean much if you didn't have a tool that can process and return query results. One such tool that I've found to be particularly useful is Saxon, which was developed by Michael Kay. Saxon comes in two versions, a commercial version and a free open source version. You'll find that the free open source version of Saxon will probably serve your needs just fine, at least for now as you learn the ropes of XQuery. If you have some grand plans for XQuery in the works, by all means check into the full commercial version of XQuery as well. You can find both versions online at http://www.saxonica.com/.

Saxon is a command-line tool that you use to execute XQuery queries. Saxon is built as a Java application, so you'll need to have the Java runtime installed in order to run Saxon. You can download the Java runtime (J2SE) from http://java.sun.com/j2se/1.4.2/download.html. After downloading and installing both Java and Saxon, it's important to set the Java CLASSPATH environment variable before attempting to run Saxon. The purpose of this variable is to let Java know where all of the executable class files are located when it tries to run an application. In the case of Saxon, you're interested in letting Java know about the file saxon8.jar, which is located in the main Saxon folder, usually \saxon.

To set the CLASSPATH variable for Saxon, just issue this command at the command line:

set classpath=%classpath%;\saxon\saxon8.jar

You may want to double-check the Saxon documentation for your specific version to make sure the .JAR file has the same name as I've mentioned here (saxon8.jar). If not, just change the code to match the file you have.

You're now ready to run Saxon but you need to know how to run it. However, before you get to that you need to understand how queries are stored for XQuery. XQuery documents are stored in files with a .XQ file extension. In addition to the query code, all XQuery documents are required to start with the following line of code:

xquery version "1.0";

The query code then follows after this line. So you have an XQuery document with a .XQ file extension and an XML file with probably a .XML file extension, and you're ready to run the query on the XML document. Just issue the following command at the command line:

java net.sf.saxon.Query -s xmldoc.xml querydoc.xq > outdoc.xml

In this sample command, xmldoc.xml is the XML source document, querydoc.xq is the XQuery document, and outdoc.xml is the output document that the query results are written to. There are numerous other options you can use with Saxon but this basic command is all you need to get going running your own queries.