XQuery
XQuery | |
---|---|
Paradigms : | XML |
Publishing year: | 2005-04-04 |
Developer: | World Wide Web Consortium |
Current version : | 3.1 (March 21, 2017) |
Typing : | strong |
Important implementations : | BaseX , EXist (database) , Galax, Saxon , Pathfinder, XQilla, Zorba |
Standardizations: | W3C specifications |
Influenced by: | XQL , XML-QL , Quilt |
Operating system : | platform independent |
w3.org |
XQuery stands for XML Query Language and describes a query language specified by the W3C for XML databases . It is used to pick out individual parts from large XML data collections. In contrast, XSLT is used to transform entire XML documents.
XQuery uses a syntax based on XSLT, SQL and C and uses XPath and XML Schema for its data model and its function library . It emerged from the languages XQL , XML-QL and Quilt .
XQuery is strongly typed and Turing-complete .
Donald D. Chamberlin played a key role in the development (as with Quilt) .
Language elements
In addition to path expressions ( XPath ), there are a number of other language properties that will be explained in the following sections using brief examples.
Data model
The basic data structure in XQuery is a sequence. A sequence is an ordered list of zero, one or more elements. A sequence can therefore also be an XML document. Sequences are usually bracketed and can also contain duplicates. Sequences cannot be nested.
The following sequences are identical:
-
(1, 2, 1)
and(1, (2, 1))
-
(1, (), )
and(1, )
-
(<A/>)
and<A/>
XQuery provides six functions for querying cardinalities on sequences:
- fn: zero-or-one ($ seq)
- fn: one-or-more ($ seq)
- fn: exactly-one ($ seq)
- fn: empty ($ seq)
- fn: exists ($ seq)
- fn: count ($ seq)
In XQuery, variables are identified with a $ prefix.
Editing of sequences
- The comma operator hangs two sequences in a row
- You can add values with the function fn: insert-before
- You can delete values with the function fn: remove
- You can rearrange values with the function fn: reverse
- You can rearrange values with the function fn: unordered
Construction of XML elements
Direct XML construction with constant element names ("direct constructors"):
<html> <head> { $page/head/content() } </head> <body bgcolor={ $style/bgcolor/text() } > { $page/body/content() } </body> </html>
"Computed constructors"
element html { element head { $page/head/content() } element body { attribute bgcolor { $style/bgcolor/text() }, text { $page/body/content() } } }
Both the direct notation as XML and the more declarative construction using the so-called “computed constructors” are available for constructing XML data. In both cases, the curly braces are used { ... }
to embed any other XQuery expressions in the constructor.
Sort functionality ("order by")
In contrast to relational algebra and SQL , XML data elements have an implicitly specified order (“document order”). All XQuery expressions must have this order, unless this is explicitly switched off in the expression (Ordering mode: unordered).
FLWOR expressions
The so-called FLWOR expressions (pronounced: flower ) play a central role in XQuery . FLWOR is an abbreviation for the constructs for, let, where, order by
and return
, and can be viewed as an analogy to the (SELECT, FROM, WHERE) constructs in SQL. In contrast to SQL, however, FLWOR expressions are case-sensitive . FLWOR expressions map sequences to sequences (see data model ). FLWOR expressions have the following generic form:
for $forvar1 at $posvar1 in <Expr>, $forvar2 at $posvar2 in <Expr> … let $letvar1 := <Expr>, $letvar2 := <Expr> … where <BoolExpr> order by <Expr> ascending/descending return <Expr>
Here stands <Expr>
for any other XQuery expression, and <BoolExpr>
for an expression of the type boolean. The for
constructs bind their variables $forvar1, $forvar2, ...
sequentially to a value from the binding sequences <Expr>
. The at
position of the preceding variable in the <Expr>
sequence can be linked to a position variable using the keyword , with the numbering starting at 1. In contrast, the let
constructs bind their variables $letvar1, $letvar2, ...
to the entire result of the associated expression. The resulting tuple sequence contains all possible combinations of variable bindings that can be formed from the assignment of the for and let variables.
The where
construct is used to eliminate unwanted tuples. It makes sense to use its Boolean expression to refer to at least one of the variables bound in the for
and let
constructs. The same applies to the order by
construct used to sort the tuples. Finally, in the return
construct, the variables of interest are returned, possibly embedded in directly or indirectly constructed elements. In more complex cases, FLWOR expressions are also nested, which means that <Expr>
any FLWOR expression can be used instead of each occurrence of .
The following example illustrates the use of for
and let
. The for
part of the printout selects all paragraphs within an HTML page (<p> elements). In the let
part, the number of words is determined for each paragraph with the help of predefined functions. Then all non-empty paragraphs (word count> 0) are extended by a size specification and output.
for $par in $page//p let $words := fn:count(fn:tokenize($par/content(), " \n\t")) where $words gt 0 return <p> {$par/content()}<br/> Size: { $words } </p>
It should be noted with this expression that the for
and let
part are dependent on each other, the for
variable declared $par
in the let
part is used in the part. For the evaluation, this means that the let
-part $par
must be re-evaluated for each assignment of the variable .
Compound operations and groupings
Compound operations (or joins ) denote a set operation consisting of the formation of a Cartesian product of two input sets and a subsequent selection (see Relational Algebra ). Such a compound operation is already shown in the example of the previous section on FLWOR expressions. The linkages of the variables declared in the for
and let
parts form the respective input where
quantities to whose Cartesian product the selection conditions of the part are applied. Since selection conditions can occur both in the where
-part of the expression and within the for
- and let
-part in subexpressions, normalization is necessary before evaluating an expression.
Predefined functions
For calculating arithmetic equations and date calculations as well as for processing sequences in the data model .
Custom functions
User- declare function
defined functions can be declared using. The general form looks like this:
declare function namespace:funktionsname ($parameter1 as datentyp1, $parameter2 as datentyp2, …) as rückgabedatentyp { <XQuery-Ausdruck> }
Functions can recursively call other functions and themselves , so XQuery is Turing complete .
Different comparison operators
XQuery knows two types of comparison functions: value qualifying or existence qualifying. The value-qualifying operators are similar to the usual operators from other programming languages. The following operators are defined
- eq: Checks for equality
- ne: Checks for inequality (not equal)
- lt: Checks for less than
- gt: Checks for greater than
- le: Checks for less or equal
- ge: Checks for greater or equal
The "normal" comparison operators ( =
, !=
, <
, >
, <=
, >=
) through an existential comparison. There can be sequences to the left and right of the operator. So the expression is seq1 OP seq2
only true if an element e1
of seq1
and e2
from seq2
there to e1 OP e2
where OP
one of the operators =
, !=
, <
, >
, <=
or >=
is. So is (1, 2) > (3, 0)
true there 1 > 0
is.
Examples
Counting all question XML elements in the source document:
fn:count(//question)
More complex example, listing all questions of all documents in a gallery for KEduca that have less than two answers:
<noanswerquestions>{
for $s in fn:doc("lpi101.edugallery")//server/@address
for $d in fn:doc($s)[count(.//question/(true, false)) <= 1]
return <doc src="{ $s }">{
$d//question[count((true, false)) <= 1]
}</doc>
}</noanswerquestions>
Another example (shows the possibility of indirect restructuring through XQuery)
- Source document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<partlist>
<part partid="0" name="car"/>
<part partid="1" partof="0" name="engine"/>
<part partid="2" partof="0" name="door"/>
<part partid="3" partof="1" name="piston"/>
<part partid="4" partof="2" name="window"/>
<part partid="5" partof="2" name="lock"/>
<part partid="10" name="skateboard"/>
<part partid="11" partof="10" name="board"/>
<part partid="12" partof="10" name="wheel"/>
<part partid="20" name="canoe"/>
</partlist>
- Target document:
<parttree>
<part partid="0" name="car">
<part partid="1" name="engine">
<part partid="3" name="piston"/>
</part>
<part partid="2" name="door">
<part partid="4" name="window"/>
<part partid="5" name="lock"/>
</part>
</part>
<part partid="10" name="skateboard">
<part partid="11" name="board"/>
<part partid="12" name="wheel"/>
</part>
<part partid="20" name="canoe"/>
</parttree>
- Solution in XQuery:
declare function local:one_level ($p as node()) as node()
{
<part partid="{$p/@partid}" name="{$p/@name}">
{
for $s in doc("data/parts-data.xml")//part
where $s/@partof =$p/@partid
return local:one_level($s)
}
</part>
};
<parttree>
{
for $p in doc("data/parts-data.xml")//part [empty(@partof)]
return local:one_level($p)
}
</parttree>
application
Due to the importance of XML and historically high volumes of data in relational databases , the ISO developed an extension of the SQL standard called SQL / XML in order to combine the possibilities of XML and SQL. XQuery was defined as the query language for querying XML data in the context of the SQL function XMLQuery.
Implementations
- BaseX , Open Source: XQuery 3.1, XQuery Full-Text 1.0, XQuery Update 1.0
- eXist , Open Source: XQuery 3.0, XQuery Update 1.0
- Saxon , Open Source and Commercial: XQuery 3.1, XQuery Update 1.0
- Sirix , Open Source: XQuery 1.0 (and XQuery Update 1.0 based on Brackit )
- xqerl , Open Source: XQuery 3.1, XQuery Update 3.0
- XQilla , Open Source: XPath 2.0, XQuery Update 1.0
- Zorba , Open Source: XQuery 3.0, XQuery Full Text 1.0, XQuery Update 1.0;
- XQuery Test Suite Results : XQuery 1.0 Compliance
literature
- Priscilla Wamsley: XQuery . O'Reilly Media, Beijing 2007, ISBN 978-0-596-00634-1 .
- Margit Becher: XML - DTD, XML-Schema, XPath, XQuery, XSLT, XSL-FO, SAX, DOM . W3L Verlag, Witten 2009, ISBN 978-3-937137-69-8 .
Web links
- XQuery specification
- XQuery Use Cases (application areas)
- XQuery Update Facility
- In-depth introduction to XQuery
Individual evidence
- ↑ Michael Wagner: SQL / XML: 2006 - Evaluation of the standard conformity of selected database systems . Diplomica Verlag, 2010, ISBN 3-8366-9609-6 .