Before you start
This tutorial teaches you how to use XQuery to retrieve information from an XML document. It also explains the ways in which XPath changes with version 2.0, and what those changes mean for data management.
You should be familiar with XML. An understanding of XPath 1.0 is helpful, but not required.
As XML matures as a format for storing data, the importance of an SQL-like language to retrieve information from a non-structured format such as XML documents grows. XML Query, or XQuery, is a response to this requirement. It utilizes several different structures and ideas, including significant enhancements to XPath that were developed for XSLT 2.0.
With XQuery, you can select very precise sets of data from an XML document or documents, and output an XML structure in response. It also includes sophisticated type management and the ability to create queries that adapt to different situations.
This tutorial guides you through the process of creating and understanding different types of queries available with XQuery and the changes in XPath 2.0. It progresses as follows:
- XQuery basics: Provides an overview of the topic, with brief explanations of each topic.
- Using XQuery with DB2: Walks you through setting up an IBM® DB2® V9 database for XQuery queries of stored XML.
- FLWOR statements: Explains the use of FOR-LET-WHERE-ORDER-RETURN statements, which are used in a manner similar to SQL.
- XPath expressions: Examines the ways in which XPath expressions select specific nodes from an XML document.
- Sequences: Looks at sequences, which replace XPath 1.0's node-sets, and some of the operations and functions that can act on them.
- Additional features: Explains some of the more advanced constructions available in an XQuery expression.
- XQueryX: Explains the XML-structured form of XQuery that corresponds to the human-readable form.
The output for all queries is shown in the tutorial, so it's not necessary to actually run them in order to understand the concepts discussed. If, however, you do want to run some of the queries, you can download and install IBM DB2 version 9, which includes PureXML™ capabilities that make it possible to embed XQuery in an SQL statement. The code in this tutorial was tested on DB2 Enterprise 9.
You can download a trial version of DB2 9: DB2 Enterprise 9 or DB2 Express-C 9, a no-charge version of the DB2 Express 9 data server.



