Using XQuery

W3C XQuery is a query language for XML. The most common use cases for XQuery involve XML publishing to create XML for Web messages, dynamic web sites, or publishing applications. The original data may be found in XML files, or it may be in a data store such as a relational database.

Some of the queries in this tutorial operate on XML stored in files, some on an XML view of a relational database, and some work on both. All of the examples in this tutorial have been tested with DataDirect XQuery. Because all XQuery implementations do not access relational data in the same way, this tutorial uses fn:collection(), which DataDirect XQuery uses to access relational tables.

Most XQuery functionality, such as arithmetic operators, comparisons, function calls, and functions, is familiar to most programmers. This tutorial focuses on the three major capabilities of XQuery that make it distinctive, and which are fundamental to processing and creating XML.

  • Path expressions, which can locate anything in an XML document.
  • XML constructors, which can create any XML document.
  • FLWOR expressions (pronounced "flower expressions"), which allow data to be combined to create new XML structures. They are similar to SQL Select statements that have From and Where clauses, but are adapted for XML processing.

Together, these capabilities make it easier to process and create XML with XQuery than with other languages when using data from XML or relational sources.

Table of Contents

Finding XML Nodes: Path Expressions

Just as SQL needs to be able to access any row or column in a relational table, XQuery needs to be able to access any node in an XML document. XML structures have both hierarchy and sequence, and can contain complex structure.

Creating XML: XML Constructors

Now that we have seen how to locate anything in an XML document or a relational table, let's learn how to create new XML structures using XML constructors.

Restructuring Data: FLWOR Expressions

XQuery has an expression called a FLWOR expression, which is similar to a SQL Select statement that that has From and Where clauses. FLWOR is pronounced "flower", and is an acronym for the keywords used to introduce each clause (for, let, where, order by, and return).


The three major capabilities of XQuery that make it distinctive are the ability to:

  • Locate any content in an XML document
  • Create any XML document
  • Combine data to create new XML structures using FLWOR expressions

For a more extensive XQuery tutorial, see XQuery: A Guided Tour.

DataDirect XQuery, an implementation of XQuery, allows you to query both relational and XML sources and combine the data into one result.