Before you start
Before you examine XQuery code samples, here's how to get the most of this tutorial, and instructions on how to install and use the included source code (see Downloads).
This tutorial is about using XQuery to develop applications and middleware. It outlines some of XQuery's limitations while you develop applications, gives you practical advice along the way on how to manage these limitations as well as highlights where XQuery makes it easy or difficult. The bulk of the tutorial then builds on these principles, as it presents a series of programmatic idioms commonly found in application development:
- Use of extension functions
- Unit testing and assertions
- Recursion and sorting
- Higher-order functions
Each section comes with accompanying source code examples (see Downloads).
The goal of this tutorial is to give you a practical grounding in how to develop applications with XQuery. I try to provide cut-and-paste code that you can re-use in your own application-development efforts, though it is my hope that these code examples will inspire you to reconsider using XQuery for more than querying XML.
This tutorial is written for developers who have a general familiarity with XML technologies as well as some cursory XSLT or Query experience. The programmatic idioms presented can be found in one form or another in a lot of computer programming languages, and I make no claim of invention on them. This re-use should mean that for most readers, you'll see familiar constructions—albeit set in the context of XQuery programming.
You must install Michael Kay's Saxon
XSLT and XQuery processor SA aware version to execute code examples.
As you need the Saxon-SA version, you must register for a 30-day trial
(for example, at the time of this writing version 9.1SA was used in testing).
Many of the code examples use higher-order
functions to take advantage of specific Saxon-SA extension
functions (specifically, the
Place all the Java™ Archive (JAR) files and license file in Saxon under the /lib directory.
To run examples you can invoke Saxon from the command line or use the included Ant
build file (which I used to test the code examples). If you do use the build file, then you should also install the latest Apache Ant,
and take care to amend the
property to point to the /lib directory containing Saxon JARs.
To test whether Saxon is working properly, run the Ant target
checkSaxon, which will successfully process when Saxon is installed
properly. All Ant targets generate result output into the /result directory.