Difference between revisions of "XQuery 3.0"
Line 6: | Line 6: | ||
==EQName: Expanded QNames== | ==EQName: Expanded QNames== | ||
− | A ''QName'' can now be directly prefixed with | + | {{Mark|Updated with Version 7.2:}} |
+ | |||
+ | A ''QName'' can now be directly prefixed with the letter "Q" and a namespace URI in the [http://www.jclark.com/xml/xmlns.htm Clark Notation]. | ||
'''Examples:''' | '''Examples:''' | ||
− | * <code><nowiki> | + | * <code><nowiki>Q{http://www.w3.org/2005/xpath-functions/math}pi()</nowiki></code> returns the number π |
− | * <code> | + | * <code>Q{java:java.io.FileOutputStream}new("output.txt")</code> creates a new Java file output stream |
− | + | Before {{Mark|Version 7.2}}, and in older versions of the XQuery 3.0 specification, the prefixed namespace URI was quoted: | |
− | * <code><nowiki> | + | * <code><nowiki>"http://www.w3.org/2005/xpath-functions/math":pi()</nowiki></code> |
− | * <code> | + | * <code>"java:java.io.FileOutputStream":new("output")</code> |
==Namespace Constructors== | ==Namespace Constructors== |
Revision as of 02:26, 13 March 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
A QName can now be directly prefixed with the letter "Q" and a namespace URI in the Clark Notation.
Examples:
Q{http://www.w3.org/2005/xpath-functions/math}pi()
returns the number πQ{java:java.io.FileOutputStream}new("output.txt")
creates a new Java file output stream
Before Template:Mark, and in older versions of the XQuery 3.0 specification, the prefixed namespace URI was quoted:
"http://www.w3.org/2005/xpath-functions/math":pi()
"java:java.io.FileOutputStream":new("output")
Namespace Constructors
New namespaces can now be created via so-called 'Computed Namespace Constructors'.
element node { namespace pref { 'http://url.org/' } }
String Concatenations
Two vertical bars ||
(also names pipe characters) can be used to concatenate strings. This operator is a shortcut for the ft:contains()
function.
'Hello' || ' ' || 'Universe'
External Variables
Default values can now be attached to external variable declarations. This way, an expression can also be evaluated if its external variables have not been bound to a new value.
declare variable $user external := "admin"; "User:", $user
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 XPTY0004 { 'Typing error: ' || $err:description } catch * { 'Error [' || $err:code || ']: ' || $err:description }
Result: Typing error: '+' operator: number expected, xs: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 := document { <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
Changelog
Version 7.1
- Expanded QNames, Computed Namespace Constructor
Version 7.0
- String Concatenator