XQuery speeds up the process of finding information contained in an XML document, which is very handy when dealing with long XML documents. This article, the first of two parts, will teach you how to write XQuery expressions. It is excerpted from chapter nine of the book XML DeMYSTiFieD, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109).
A customer calls you asking a question about her order. You need to quickly access her order; however, all orders are stored in a very long XML document. Itís too time-consuming to search through each line of the XML document. What do you do? You could panic and have an unhappy customer, or you can create an XQuery to electronically search for and display the customerís order.
Think of XQuery as your electronic assistant who knows where to find any information in an XML document as fast as your computer will allow. Your job is to use the proper expression to request the information. XQuery interprets your request and retrieves the information that you need from the XML document.
The way you use XQuery is similar to how you use SQL to access information from a relational database. The relational database is like an XML documentóboth contain lots of information, making it inefficient to search by hand.
In this chapter, youíll harness the power of XQuery by learning how to write expressions that enable you to tap into the vast treasure trove of information stored in an XML document.
There are a few things that youíll need to do before you write your first XQuery expression. XQuery is an extension of XPath; therefore, make sure that you read Chapter 6 before moving ahead with this chapter. Youíll also need an XQuery processor. An XQuery processor is the software thatís like an electronic assistant who searches an XML document for information you request. Skip this section if you already have installed an XQuery processor; otherwise, read on and learn how to install one.
Several XQuery processors are available. Some are freeware or open source, and others are commercial software products. Letís save a few dollars. Download the Saxon-B version 8, which is an open source XQuery processor, at:
Saxon-B is a zip file. Download it to c:\saxon and then unzip the file. Thatís all you need to do to install Saxon-B.
Saxon-B is a Java program. Therefore, youíll also need to have a runtime version of Java installed on your computer. Youíll find a free, downloadable copy at:
Download the latest version of Java 2 Standard Edition (J2SE) and follow the installation instructions that come with the downloaded file. You donít need to know anything about Java to run Saxon-B with the examples we show in this chapter. Take note of where you install the Java runtime. You will need to know this to execute the samples.