- An introduction to XQuery
- XPath and XQuery Tutorial | Altova
- Two diverse constituencies
- Introduction to XQuery
The set of documents that in their totality describe and define XQuery currently consists of:. These documents all referenced in Related topics represent a prodigious body of work. XQuery 1.
An introduction to XQuery
To my knowledge this is the most complex set of specifications to come out of the W3C although XML Schema arguably comes to mind, but that's another story If you're staring at this mass of documentation for the first time and wondering where to begin, I can recommend two possible approaches. You can start with the central XQuery 1. It has a good, introductory overview and details each of the language's many, many features. Another approach is to begin by picking up the Use Cases working draft. This document outlines a number of real-world scenarios where XQuery has applicability.
Each use case targets a specific application domain and lists a number of XQueries posed against the sample data for that domain. The code snippets are invaluable if you like looking at concrete examples of real working syntax. A third approach, which works best if you already have a minimal understanding of the language, is to look through the many built-in functions listed in the Functions and Operators working draft. Two excellent books have also appeared on the scene in the last several years to explain the ins and outs of the specification, both from Addison-Wesley: "XQuery from the Experts" presents a number of detailed technical essays on XQuery-related topics from members of the working group, while "XQuery: The XML Query Language" is an imminently readable reference work by Microsoft's Michael Brundage see Related topics.
The two documents detail a query algebra, a set of precise definitions that define in formal terms the core entities that an XQuery query is expected to operate on, and formulations of what the various language operators can do with those operands. This likely won't be of interest to you unless you're a query-engine implementer, have major pocket protection, or simply like working with complex, formal systems.
One mapping that's provided enables implementers to recast surface syntax features directly into the underlying algebra. You can implement query processors that actually speak the algebra directly although I would think this is more for proof-of-concept , as several vendors have demonstrated at XML trade shows. A link in Related topics points to an online demo version of one of these algebra-based engines.
The algebra also provides rules that detail how to optimize and transmute both complex expressions into simpler equivalents. As best I can tell I'm not a language theorist, and the Formal Semantics document is far from light reading , both of these are good things. Large database vendors in particular will appreciate a query-language architecture that's designed from the ground up to be both optimizable and efficient. The algebra also provides a place to hang type information.
The algebra has both static and dynamic typechecking capability. For example, an engine can use PSVI-derived type information to statically check the datatype of query expressions at compile time when the query's being analyzed for syntactic correctness.
XPath and XQuery Tutorial | Altova
Determining that a query is type-invalid early in the cycle short circuits the need for doing potentially expensive and fruitless searches against large datasets. Much of the work on the XQuery specification has involved work on the syntax and semantics of the part of the language involving types. XQuery shares a common data model with XPath 2. XPath 2. The data model describes the core information in an XML document that's of interest to an XPath processor, and the final syntax and semantics of XPath's step operations is now almost completely worked out.
At times that's been challenging, both politically and technically. However, if the road to consensus is sometimes a rocky one, both groups seem to be navigating it without too much obvious discomfort at least as seen from an outsider's possibly naive perspective.
As just one example of why the transition from XPath 1.
- Primitive data types?
- Introduction to XQuery Tutorial: A Training Course.
- Stay ahead with the world's most comprehensive technology and business learning platform..
- Recuerdos ocultos (Deseo) (Spanish Edition);
- An Introduction to XQuery.
- XQuery Tutorial!
Node-sets, one of the four datatypes in XPath 1. By definition, sets are unordered and contain no duplicate members. By contrast, sequences of nodes in XPath 2. The ramifications of these differences were among a number of issues that had to be hammered out by the working groups separately and in concert as they brought themselves and XPath 2. All of the substantive remaining issues have now been worked out.
The fact that seven of the key documents comprising the XQuery spec are now Candidate Recommendations in W3C parlance means, in official terms, that XQuery is now considered "stable and appropriate for implementation. In terms of the formal W3C Recommendation process, all issues raised during the previous Last Call period have been responded to, and the working group is now looking to industry vendors to provide real-life verification that XQuery's major features are implementable.
To do this, implementers run their implementations through a test suite that's provided by the working group. Those features that aren't implemented by two or more vendors during the Candidate Recommendation stage are at risk of being dropped from the specification. The current list of at-risk features includes:.
XQueryX, the specification of an alternative XML-based syntax for the surface language, was one of the earlier additions to the XQuery document family. One of the requirements for XQuery states that multiple syntaxes might be possible -- it sounds as if the working group was hedging its bets a bit -- and if so, one of these would have to be convenient for humans to read and write; the other would have to be expressible in XML. XQueryX is the working group's answer to the latter requirement. Having an XML-based query representation has all the obvious, known advantages of XML: It makes it easy for standard tools to parse, generate, and interrogate the contents of a query.
This might be useful, for example, if you're doing source-level optimization or transformation, which might depend in turn on the ability to easily inspect a query for a particular grammatical structure. XML is good at such tasks. Given the complexity of the grammar, this makes XQueryX highly verbose, to the degree that it's nearly impossible for humans to read. Happily, machines -- which are the intended recipients of the language -- don't complain about such things.
Listings 1 and 2 provide a comparison of a simple query expressed first in standard XQuery syntax and then in its XQueryX counterpart. Note the significant steroid-like bulk-up factor. Listing 2 shows the XQueryX equivalent. I've omitted about three-quarters of the listing due to its length.
The full listing, lifted directly from the XQueryX working draft, runs to lines:. When I first wrote this summary of existing XQuery implementations in June , just after the first major publishing iteration, only two implementations were available: a very early one of my own, and Microsoft's.
Numbers, including integers and floating-point numbers. The boolean values true and false. Strings of characters, for example: "Hello world! These are immutable - i. 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 like xsl:template after it has been namespace-resolved.
Derived types are variations or restrictions of other types, for example range 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 of course also has the necessary data types needed to represent XML values.
Two diverse constituencies
It does this using node values, of which there are 7 kinds: element, attribute, namespace, text, comment, processing-instruction, and document root nodes. 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 root element is a child of the root node.
You can also create new node objects directly in the program. 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.
Introduction to XQuery
Note that a sequence containing just single value is the same as that value by itself, and 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. Many of the standard functions for working with nodes return sequences. In fact, XQuery can be viewed as a generalization of XPath: Except for some obscure forms mostly unusual "axis specifiers" , all XPath expressions are also XQuery expressions.
The following simple example assumes an XML file "mybook. For compatibility, these sequences will be in document order and with duplicates removed, which makes them equivalent to sets, By the way: XPath expressions are mostly used as patterns in XSLT stylesheets.
XSLT is very useful for expressing very simple transformations, but more complicated stylesheets especially anything with non-trivial logic or programming can often be written more compactly and readably using XQuery. The value of the entire for expression is the concatenation of all values of the return expression, in order. So the example evaluates to this 6-element sequence: 1,11,2,12,3,13 Here is a more useful example. Assume again that mybook.
A where clause causes the result clause to be evaluated only when the where expression is true. Below is an example illustrating the where clause. This example has a nested loop, allowing us to combine two sequences, one of customer elements , and the other of order elements.