|
Resources | Buyer's Guide | Newsletter | Safari Bookshelf | ||
Edit and debug XSLT 1.0/2.0 and XQuery code - Download Altova XMLSpy 2005 today! | ||
Annotated XML What is XML? What is XSLT? What is XSL-FO? What is XLink? What is XML Schema? What is XQuery? What is RDF? What is RSS? What are Topic Maps? What are Web Services? What are XForms? XSLT Recipe of the Day
Manage Your Account
Syntax Checker |
What is XQuery?by Per BothnerOctober 16, 2002 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 LanguageThe 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
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 $x := 5 let $y := 6 return 10*$x+$yThis evaluates to 56. Primitive Data TypesThe primitives data types in XQuery are the same as for XML Schema.
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 ExpressionsXQuery 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 Various standard XQuery functions create or return nodes. The
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 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). SequencesWe'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
let $a := 3,4 let $b := ($a, $a) let $c := 99 let $d := () return (count($a), count($b), count($c), count($d)) evaluates to Many of the standard functions for working with nodes return
sequences. The children(<p>This is <em>very</em> cool.</p>) returns this sequence of 3 values: "This is ", <em>very</em>, " cool."
|
Sponsored By: |
|
|
Contact Us | Our Mission | Privacy Policy | Advertise With Us | | Submissions Guidelines | |
Copyright © 2005 O'Reilly Media, Inc. |