Difference between revisions of "XQuery 3.0"
LeoWoerteler (talk | contribs) |
LeoWoerteler (talk | contribs) |
||
Line 137: | Line 137: | ||
</ul> | </ul> | ||
− | Function items can also be passed as arguments to and returned as results from functions. These so called | + | Function items can also be passed as arguments to and returned as results from functions. These so called [[Higher-Order Functions]] like <code>fn:map</code> and <code>fn:fold-left</code> are discussed in more depth on their own Wiki page. |
==Serialization== | ==Serialization== |
Revision as of 04:13, 27 February 2012
This article is part of the Query Portal. It summarizes features of the upcoming XQuery 3.0 and XPath 3.0 Recommendations that have already been implemented in BaseX.
Contents
EQName: Expanded QNames
Template:Mark A QName can now be directly prefixed with its namespace URI, which will be enclosed in quotes and followed by a colon.
Examples:
"http://www.w3.org/2005/xpath-functions/math":pi()
returns the number π"java:java.io.FileOutputStream":new("output")
creates a new Java file output stream
Namespace Constructors
Template:Mark New namespaces can now be created via so-called 'Computed Namespace Constructors'.
element node { namespace pref { 'http://url.org/' } }
String Concatenations
Template:Mark Two vertical bars ||
(also names pipe characters) can be used to concatenate two strings. This operator is a shortcut for the ft:contains()
function.
'Hello' || ' ' || 'Universe'
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 XMark 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 } { $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
Function Items
One of the most distinguishing features added in XQuery 3.0 are function items, also known as lambdas or lambda functions. They make it possible to abstract over functions and thus writing more modular code.
Examples:
Function items can be obtained in three different ways:
- Declaring a new inline function:
let $f := function($x, $y) { $x + $y } return $f(17, 25)
Result:
42
- Getting the function item of an existing (built-in oder user-defined) XQuery function. The arity (number of arguments) has to be specified as there can be more than one function with the same name:
let $f := math:pow#2 return $f(5, 2)
Result:
25
- Partially applying another function or function item. This is done by supplying only some of the required arguments, writing the placeholder
?
in the positions of the arguments left out. The produced function item has one argument for every placeholder.let $f := fn:substring(?, 1, 3) return ( $f('foo123'), $f('bar456') )
Result:
foo bar
Function items can also be passed as arguments to and returned as results from functions. These so called Higher-Order Functions like fn:map
and fn:fold-left
are discussed in more depth on their own Wiki page.
Serialization
Serialization parameters can now be defined within XQuery expressions. 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>
Context Item
The context item can now be specified in the prolog of an XQuery expressions:
Example:
declare context item := <xml> <text>Hello</text> <text>World</text> </xml>; for $t in .//text() return string-length($t)
Result: 5 5
Functions
BaseX supports all functions that have been added in Version 3.0 of the XQuery Functions and Operators Working Draft. The new functions are listed below:
math:pi()
,math:sin()
, and many others (see Math Module)fn:analyze-string()
fn:available-environment-variables()
fn:element-with-id()
fn:environment-variable()
fn:filter()
fn:fold-left()
fn:fold-right()
fn:format-date()
fn:format-dateTime()
fn:format-integer()
fn:format-number()
fn:format-time()
fn:function-arity()
fn:function-lookup()
fn:function-name()
fn:generate-id()
fn:has-children()
fn:head()
fn:innermost()
fn:map()
fn:map-pairs()
fn:outermost()
fn:parse-xml()
fn:path()
fn:serialize()
fn:tail()
fn:unparsed-text()
fn:unparsed-text-available()
fn:unparsed-text-lines()
fn:uri-collection()
New signatures have been 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