8 Query Languages
For relational databases the standard query language is SQL. The main pur-
pose of SQL is to select records from one (or more) tables according to se-
lection criteria. Having selected the relevant records, one can then extract
the required information from the fields of the relevant records. Because of
the success and popularity of SQL, it was natural to imitate SQL when a lan-
guage was developed for querying XML. However, XML documents have a
hierarchical structure that relational databases do not possess. Consequently,
XML querying involves three kinds of operation:
- Navigation.This is the process of locating an element or attribute within
the hierarchical structure of an XML document.
- Selection. Having located desirable elements and attributes, one selects
the relevant ones.
- Extraction.The last operation is to extract required information from the
relevant elements and attributes.
The first kind of operation is unique to XML querying, while the other two
are similar to what one does in SQL.
Navigation is so important to XML that a separate language was devel-
oped to deal with it, calledXPath(W3C 1999). This language is introduced
in section 8.1. XPath has been incorporated into many other languages and
tools, and so it is widely available. One such language isXQuery(W3C 2004c)
which is the standard query language for XML, covered in section 8.2. If one
has some experience with relational databases and SQL, it will look famil-
iar, although there are a few differences. The main difference is that XQuery
supports navigation using XPath. Indeed, a query using XQuery can consist
of nothing more than an XPath expression, and in many cases that is all one
needs.