Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Process XML using XQuery

Learn to more easily and naturally search your XML data with XQuery 1.0

Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, Florida, USA, and is the author of three books on Web development, including Java and XML From Scratch (Que) and the upcoming Primer Plus XML Programming (Sams). He loves to hear from readers and can be reached at nicholas@nicholaschase.com.

Summary:  For years developers have used SQL to retrieve data from structured sources such as relational databases. But what about unstructured and semi-structured sources, such as XML data? To be viable as a data source, XML needed a means to conveniently retrieve the data. XQuery provides this means, allowing developers to write a statement that both extracts data and (if necessary) structures the results as XML. This tutorial shows you how to use XQuery to retrieve information from an XML document stored in an XQuery-enabled database. It also explains the ways in which XPath changes with version 2.0, and what those changes mean for data management.

Date:  27 Mar 2007 (Published 24 Sep 2002)
Level:  Intermediate PDF:  A4 and Letter (390 KB | 43 pages)Get Adobe® Reader®

Activity:  26054 views
Comments:  

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.

What is this tutorial about?

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.

Prerequisites

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.

1 of 11 | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Information Management
ArticleID=138525
TutorialTitle=Process XML using XQuery
publish-date=03272007
author1-email=nicholas@nicholaschase.com
author1-email-cc=