Difference between revisions of "XQuery 3.0"
(→Group By: added example) |
|||
Line 3: | Line 3: | ||
==Group By== | ==Group By== | ||
− | FLWOR expressions have been extended | + | FLWOR expressions have been extended to include the [http://www.w3.org/TR/xquery-30/#id-group-by group by] clause, which is well-established among relational database systems. <code>group by</code> can be used to apply value-based partitioning to query results: |
'''Example:''' | '''Example:''' | ||
Line 14: | Line 14: | ||
"standard" | "standard" | ||
else if($ic >= 100000) then | else if($ic >= 100000) then | ||
− | " | + | "preferred" |
else | else | ||
"na" | "na" | ||
Line 23: | Line 23: | ||
</pre> | </pre> | ||
− | + | This query is a rewrite of [http://www.ins.cwi.nl/projects/xmark/Assets/xmlquery.txt| Query #20] contained in the [http://www.ins.cwi.nl/projects/xmark|XMark Benchmark Suite] to use <code>group by</code>. | |
The query partitions the customers based on their income. | The query partitions the customers based on their income. | ||
Line 34: | Line 34: | ||
</pre> | </pre> | ||
− | In contrast to the relational GROUP BY the XQuery counterpart | + | In contrast to the relational GROUP BY statement, the XQuery counterpart |
concatenates the values of all non-grouping variables that belong to a specific group. | concatenates the values of all non-grouping variables that belong to a specific group. | ||
In the context of our example, all nodes in <code>//people/person</code> that belong to the <code>"preferred"</code> partition are concatenated in <code class="brush:xquery">$ppl</code> after grouping has finished. | In the context of our example, all nodes in <code>//people/person</code> that belong to the <code>"preferred"</code> partition are concatenated in <code class="brush:xquery">$ppl</code> after grouping has finished. | ||
Line 40: | Line 40: | ||
<pre class="brush:xquery"> | <pre class="brush:xquery"> | ||
− | + | ... | |
return element {$income} {count($ppl)} | return element {$income} {count($ppl)} | ||
</pre> | </pre> |
Revision as of 13:12, 13 January 2011
This article summarizes the most important features of the upcoming Version 3.0 of the XQuery language that have already been implemented in BaseX.
Group By
FLWOR expressions have been extended to include the group by clause, which is well-established among relational database systems. group by
can be used to apply value-based partitioning to query results:
Example:
for $ppl in doc('xmark')//people/person let $ic := $ppl/profile/@income let $income := if($ic<30000) then "challenge" else if($ic >= 30000 and $ic<100000) then "standard" else if($ic >= 100000) then "preferred" else "na" group by $income order by $income return element {$income} {count($ppl)}
This query is a rewrite of Query #20 contained in the Benchmark Suite to use group by
.
The query partitions the customers based on their income.
Result:
<challenge>4731</challenge> <na>12677</na> <prefered>314</prefered> <standard>7778</standard>
In contrast to the relational GROUP BY statement, the XQuery counterpart
concatenates the values of all non-grouping variables that belong to a specific group.
In the context of our example, all nodes in //people/person
that belong to the "preferred"
partition are concatenated in $ppl
after grouping has finished.
You can see this effect by changing the return statement to:
... return element {$income} {count($ppl)}
Result:
<challenge> <person id="person0"> <name>Kasidit Treweek</name> … <person id="personX"> … </challenge>
Try/Catch
The try/catch construct can be used to handle errors at runtime:
Example:
try { 1 + '2' } catch *($code, $desc) { concat('Error [', $code, ']: ', $desc) }
Result: Error [XPTY0004]: '+' operator: number expected, string found.
Switch
The switch statement is available in many other programming languages. It chooses one of several expressions to evaluate based on its input value.
Example:
for $fruit in ("Apple", "Pear", "Peach") return switch ($fruit) case "Apple" return "red" case "Pear" return "green" case "Peach" return "pink" default return "unknown"
Result: red green pink
Serialization
Serialization parameters can now be defined within XQuery expressions. All available parameters are supported, which are specified in the W3C Serialization Document. Parameters are placed in the query prolog and need to be specified as option declarations, using the output
prefix.
Example:
declare option output:omit-xml-declaration "no"; declare option output:method "xhtml"; <html/>
Result: <?xml version="1.0" encoding="UTF-8"?><html></html>
Functions
This paragraph lists all new functions of the XQuery 3.0 Specification that are already supported in BaseX.
The following functions have been added:
math:pi()
,math:sin()
, and many others (see Math Functions)fn:head()
fn:tail()
fn:generate-id()
fn:analyze-string()
fn:environment-variable()
fn:available-environment-variables()
fn:unparsed-text-available()
fn:unparsed-text-lines()
fn:unparsed-text()
fn:element-with-id()
fn:parse-xml()
fn:uri-collection()
fn:serialize()
New signatures have beeen added for the following functions:
fn:document-uri()
with 0 argumentsfn:string-join()
with 1 argumentfn:node-name()
with 0 argumentsfn:round()
with 2 argumentsfn:data()
with 0 arguments
The following functions are partially supported:
fn:format-integer()
fn:format-number()
fn:format-dateTime()
fn:format-date()
fn:format-time()