What Is XQuery
The W3C is finalizing the XQuery specification, aiming for a final release in late 2002. XQuery is a powerful and convenient language designed for processing XML data. That means not only files in XML format, but also other data including databases whose structure -- nested, named trees with attributes -- is similar to XML.
XQuery is an interesting language with some unusual ideas. This article provides a high level view of XQuery, introducing the main ideas you should understand before you go deeper or actually try to use it.
An Expression Language
The first thing to note is that in XQuery everything is an
expression which evaluates to a value. An XQuery program or script is
a just an expression, together with some optional function and other
definitions. So 3+4 is a complete, valid XQuery program
which evaluates to the integer 7.
There are no side-effects or updates in the XQuery standard, though they will probably be added at a future date. The standard specifies the result value of an expression or program, but it does not specify how it is to be evaluated. An implementation has considerable freedom in how it evaluates an XQuery program, and what optimizations it does.
Here is a conditional expression that evaluates to a string:
if (3 < 4) then "yes!" else "no!"
You can define local variable definitions using a
let-expression:
let $x := 5 let $y := 6 return 10*$x+$yThis evaluates to 56.
Primitive Data Types
The primitives data types in XQuery are the same as for XML Schema.
- Numbers, including integers and floating-point numbers.
- The boolean values true and false.
- Strings of characters, for example:
"Hello world!". These are immutable - i.e. you cannot modify a character in a string. - Various types to represent dates, times, and durations.
- A few XML-related types. For example a QName is a pair
of a local name (like
template) and a URL, which is used to represent a tag name likexsl:templateafter it has been namespace-resolved.
Derived types are variations or restrictions of other types. Primitive types and the types derived from them are known as atomic types, because an atomic value does not contain other values. Thus a string is considered atomic because XQuery does not have character values.
Node Values and Expressions
XQuery also has the necessary data types needed to represent XML
values. It does this using node values, of which there are
7 kinds: element, attribute, namespace, text, comment,
processing-instruction, and document (root) nodes. These are very
similar to the corresponding DOM classes such as Node,
Element and so on. Some XQuery implementations use DOM
objects to implement node values, though implementations may use other
representations.
Various standard XQuery functions create or return nodes. The
document function reads an XML file specified by a URL
argument and returns a document root node. (The root element is a
child of the root node.)
You can also create new node objects directly in the program. The most convenient way to do that is to use an element constructor expression, which looks just like regular XML data:
<p>See <a href="index.html"><i>here</i></a> for info.</p>
You can use {curly braces} to
embed XQuery expression inside element constructors. Thus,
let $i := 2 return
let $r := <em>Value </em> return
<p>{$r} of 10*{$i} is {10*$i}.</p>
creates
<p><em>Value </em> of 10*2 is 20.</p>
Popular template processors, like JSP, ASP, and PHP, allow you to embed expressions in a programming language into HTML content. XQuery gives you that ability, plus the ability to embed XML/HTML forms inside expressions, and to have them be the value of variables and parameters.
XQuery node values are immutable (you cannot modify a node after it has been created).
Sequences
We've seen atomic values (numbers, strings, etc), and node values (elements, attributes, etc). These are together known as simple values. XQuery expressions actually evaluate to sequences of simple values. The comma operator can be used to concatenate two values or sequences. For example,
3,4,5
is a sequence consisting of 3 integers. Note that a sequence
containing just single value is the same as that value by itself. You
cannot nest sequences. To illustrate this, we'll use the
count function, which takes one argument and returns the
number of values in that sequence. So the expression
let $a := 3,4 let $b := ($a, $a) let $c := 99 let $d := () return (count($a), count($b), count($c), count($d))
evaluates to (2, 4, 1, 0) because $b is
the same as (3,4,3,4).
Many of the standard functions for working with nodes return
sequences. The children function returns a sequence of
the child nodes of the argument. Thus,
children(<p>This is <em>very</em> cool.</p>)
returns this sequence of 3 values:
"This is ", <em>very</em>, " cool."
Pages: 1, 2 |