XML Query: what is it, how do I use it and why?
Liam Quin, W3C
Copyright © 2006 W3C ®
(MIT, ERCIM,
Keio), All Rights
Reserved.
What Is It?
- A Query Language for XML
- An Integration Language
- Syntactic extension to XPath 2
- Standardised Middleware
A Query Language for XML
- Queries data stored in the XPath and XML Query Data Model
- Language uses the XML Schema type system
- Construct and manipulate XML fragments as well as regular variables
- Results are generally XML (or (X)HTML or text)
Integration Language 2

Integration Language
- Integrate and extract data from XML documents, relational data, XML databases and other sources
- Query does not need to know how or where data is stored
- Move data about without changing the query
Syntactic extension to XPath 2
- XQuery syntax is not itself XML
- Adds functions...
- ... modules (function libraries) ...
- ... new operators ...
- ... expressions (e.g. FLWOR) ...
- and more.
Standardised Middleware
Before XQuery, to connect between Web servers,
multiple databases, files, applications, you used...
Standardised Middleware [2]
PROPRIETARY
SOFTWARE
Standardised Middleware [3]
PROPRIETARY
LANGUAGES
Standardised Middleware [4]
- Easier to find staff
- reduced trining cost
- skill-set part of general XML knowlegde
- public documentation
- applications easier to maintain
- so...
Standardised Middleware [5]
IT’S BETTER.
How do I use it?
- Architecture
- Implementations
- Examples
Architecture [1]
Architecture [2]
Architecture [3]
Architecture [4]
Architecture [6]
Summary:
XML Query is a good choice for integrating
information from a wide range of sources and
producing any XML-based vocabulary.
XML Implementations tend to be optimised for
handling multiple input sources, and often use indexes
for efficiency.
Architecture [7]
IT’S FAST.
Implementations [2]
- Both open source and proprietary;
- In C
- In C++
- In Haskell
- In Java
- In vegetables with alfredo sauce
- APIs in PHP, Perl, Python, Java, ...
Examples [1]
(: find shirts that fit me :)
let $g := doc("garments.xml"),
for $shirt in $g/garments/item[type = "shirt"]
where
$shirt/collar = 15
and not(contains($shirt/style, "stripe"))
return
<candidate>$shirt/colour</candidate>
Examples [2]
(: find wallpaper images of the Tower of London :)
let $images := doc("jdbc:mysql:localhost:localuser:images"),
$kw := doc("rdf.xml")
for $item in $rdf/triplets[relation = "locname"]
where
$item/value = "Tower of London"
and some $image in $images satisfies (
$image/uri = $item/uri
and $image/width = 1024 and $image/height = 768
)
return
$item/uri
Examples [2b]
(: find wallpaper images of the Tower of London :)
for $item in $rdf/triplets[relation = "locname"]
where
$item/value = "Tower of London"
and some $image in $images satisfies (
ii:uri($image) = rdf:uri($item)
and ii:width($image) = 1024
and ii:height($image) = 768
)
return
rdf:uri($item)
Examples [2c]
declare namespace ii = "http://www.example.org/imageinfo/1.6";
declare function ii:width($image as element databaserow)
as xsi:integer?
{
return $image/width
};
Why should I use XQuery?
- Do things faster and more cheaply
- Do things you couldn’t do before
- Save money in maintenance fees
Why should I use XQuery? [2]
IT’S COOLER.
Query Status
- W3C Candidate Recommendation: no more big changes expected
- Widely implemented
- Java Community Process standardised on how to call XQuery
- ISO SQL standardised how to embed XQuery inside SQL
- Good, solid, supported implementations, both open and closed source
- IBM DB2, Microsoft SQL Server, Oracle, all include support
- Axyana, Galax, Saxonica, eXist: some open source solutions.
Why should I use XQuery? [2]
QUESTIONS?
Questions/Discussion

About
Liam Quin,
XML Activity Lead
W3C
liam@w3.org