4 Tutorial: Using XQuery
W3C defines the XML Query (XQuery) language for querying XML and combining data from documents, databases, Web pages, and other sources. Some common use cases for XQuery involve XML publishing to create XML for Web messages, dynamic web sites, and publishing applications. The source for query data might be found in XML files, relational databases, legacy files such as EDI, or from multiple combined sources.
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 not all XQuery implementations 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 that distinguish XQuery, each of which is 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 XQuery easier to use than other languages when processing and creating XML using data from XML or relational sources.
This topic covers the following subjects: