XML

What Is XQuery?

In order to explain what XQuery is, I'll first talk about the problem it's designed to solve. As you already know all too well, XML documents have a treelike structure. Let's say you wanted to find all of the elements in a document named color. Given what you've learned so far, you'd probably use the DOM to read the document into your application and then iterate over the entire tree in order to extract all the elements called color. Sounds easy enough, right? Let's say you wanted to find only the color elements that are inside elements called automobile or you want to return only those color elements that have the value blue. I think you can see where I'm going.

When you use XML for data storage, these sorts of problems are very common, so XQuery was created to simplify handling them. If you have an application that supports XQuery, you can write simple queries to perform all the tasks I mentioned in the previous paragraph and many, many more.

XQuery has been around for quite a while but has been slow to catch on. Part of this probably has to do with the fact that XML has yet to replace relational databases as the data storage medium of choice for modern applications, and it may never do so. However, XML has proven itself useful in just about every role outside of efficient data storage and retrieval, and could therefore benefit from an XML-specific query language. Enter XQuery.

For the latest information on XQuery, check out the W3C's XQuery page at http://www.w3.org/TR/xquery/.

XQuery aims to provide XML developers with the data querying power that SQL (Structured Query Language) provides to database developers. Unlike relational databases, which typically have SQL querying features built into their database management systems, you have to use a special tool in order to execute XQuery queries on XML data. This is primarily because there is no concept of a management system for XML data because the data is simply text. Later in the tutorial you learn how to use a tool to perform XML queries using XQuery.

For a look at how to use SQL to perform queries on a relational database and generate XML results, check out Using XML with Databases.

by BrainBellupdated
Advertisement: