Changes

Jump to navigation Jump to search
1,451 bytes removed ,  09:45, 30 November 2017
no edit summary
This article is part of the [[XQuery|XQuery Portal]].It summarizes provides a summary of the most interesting important features of the[http://www.w3.org/TR/xquery-30/ XQuery 3.0] Recommendations.XQuery 3.0 is fully supported by BaseXRecommendation.
=Enhanced FLWOR Expressions=
Most clauses of FLWOR expressions can now be specified in an arbitrary order: additional {{Code|let}} and {{Code|for}} clauses can be put after a {{Code|where}} clause, and multiple {{Code|where}}, {{Code|order by}} and {{Code|group by}} statements can be used. This means that many nested loops can now be rewritten to a single FLWOR expression.
'''Example:'''
for $country in db:open('factbook')//country
where $country/@population > 100000000
let $name := $country/name[1]
for $city in $country//city[population > 1000000]
group by $name:= $country/name[1]count $idreturn &lt;<country id='{ $id }' name='{ $name }'&gt;>{ $city/name }&lt;</country&gt;>
</pre>
A new {{Code|count}} clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.==group by==
<pre class="brush:xquery">for $n in (1 to 10)[. mod 2 = 1]count $creturn &lt;number count="{ $c }" number="{ $n }"/&gt;</pre> The {{Code|allowing empty}} provides functionality similar to outer joins in SQL: <pre class="brush:xquery">for $n allowing empty in ()return 'empty? ' || empty($n)</pre> Window clauses provide a rich set of variable declarations to process sub-sequences of iterated tuples. An example: <pre class="brush:xquery">for tumbling window $w in (2, 4, 6, 8, 10, 12, 14) start at $s when fn:true() only end at $e when $e - $s eq 2return &lt;window&gt;{ $w }&lt;/window&gt;</pre> More information on window clauses, and all other enhancements, can be found in the [http://www.w3.org/TR/xquery-30/#id-windows specification]. =Simple Map Operator= The [http://www.w3.org/TR/xquery-30/#id-map-operator simple map] operator {{Code|!}} provides a compact notation for applying the results of a first to a second expression: the resulting items of the first expression are bound to the context item one by one, and the second expression is evaluated for each item. The map operator may be used as replacement for FLWOR expressions: '''Example:''' <pre class="brush:xquery">(: Simple map notation :)(1 to 10) ! element node { . },(: FLWOR notation :)for $i in 1 to 10return element node { $i }</pre> A map operator is defined to be part of a path expression, which may now mix path and map operators. In contrast to the path operator, the results of the map operator will not be made duplicate-free and returned in document order. =Group By= 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 systemsin SQL. <code>group by</code> can be used to apply value-based partitioning to query results:
'''XQuery:'''
</pre>
In contrast to the relational GROUP BY statement, the XQuery counterpartconcatenates 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.You can see this effect by changing the return statement to:
<pre class="brush:xquery">
</persons>
</pre>
 
==count==
 
The {{Code|count}} clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.
 
<pre class="brush:xquery">
for $n in (1 to 10)[. mod 2 = 1]
count $c
return &lt;number count="{ $c }" number="{ $n }"/&gt;
</pre>
 
==allowing empty==
 
The {{Code|allowing empty}} provides functionality similar to outer joins in SQL:
 
<pre class="brush:xquery">
for $n allowing empty in ()
return 'empty? ' || empty($n)
</pre>
 
==window==
 
Window clauses provide a rich set of variable declarations to process sub-sequences of iterated tuples. An example:
 
<pre class="brush:xquery">
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
start at $s when fn:true()
only end at $e when $e - $s eq 2
return &lt;window&gt;{ $w }&lt;/window&gt;
</pre>
 
More information on window clauses, and all other enhancements, can be found in the [http://www.w3.org/TR/xquery-30/#id-windows specification].
 
=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 write more modular code.
 
'''Examples:'''
 
Function items can be obtained in three different ways:
 
<ul>
<li>Declaring a new ''inline function'':
<pre class="brush:xquery">let $f := function($x, $y) { $x + $y }
return $f(17, 25)</pre>
'''Result:''' <code>42</code>
</li>
<li>Getting the function item of an existing (built-in or 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:
<pre class="brush:xquery">let $f := math:pow#2
return $f(5, 2)</pre>
'''Result:''' <code>25</code>
</li>
<li>''Partially applying'' another function or function item. This is done by supplying only some of the required arguments, writing the placeholder <code>?</code> in the positions of the arguments left out. The produced function item has one argument for every placeholder.
<pre class="brush:xquery">let $f := fn:substring(?, 1, 3)
return (
$f('foo123'),
$f('bar456')
)</pre>
'''Result:''' <code>foo bar</code>
</li>
</ul>
 
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.
 
=Simple Map Operator=
 
The [http://www.w3.org/TR/xquery-30/#id-map-operator simple map] operator {{Code|!}} provides a compact notation for applying the results of a first to a second expression: the resulting items of the first expression are bound to the context item one by one, and the second expression is evaluated for each item. The map operator may be used as replacement for FLWOR expressions:
 
'''Example:'''
<pre class="brush:xquery">
(: Simple map notation :)
(1 to 10) ! element node { . },
(: FLWOR notation :)
for $i in 1 to 10
return element node { $i }
</pre>
 
In contrast to path expressions, the results of the map operator will not be made duplicate-free and returned in document order.
=Try/Catch=
</pre>
'''Result:''' <code>red red</code>
 
=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 write more modular code.
 
'''Examples:'''
 
Function items can be obtained in three different ways:
 
<ul>
<li>Declaring a new ''inline function'':
<pre class="brush:xquery">let $f := function($x, $y) { $x + $y }
return $f(17, 25)</pre>
'''Result:''' <code>42</code>
</li>
<li>Getting the function item of an existing (built-in or 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:
<pre class="brush:xquery">let $f := math:pow#2
return $f(5, 2)</pre>
'''Result:''' <code>25</code>
</li>
<li>''Partially applying'' another function or function item. This is done by supplying only some of the required arguments, writing the placeholder <code>?</code> in the positions of the arguments left out. The produced function item has one argument for every placeholder.
<pre class="brush:xquery">let $f := fn:substring(?, 1, 3)
return (
$f('foo123'),
$f('bar456')
)</pre>
'''Result:''' <code>foo bar</code>
</li>
</ul>
 
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.
=Expanded QNames=
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:'''
* <code><nowiki>Q{http://www.w3.org/2005/xpath-functions/math}pi()</nowiki></code> returns the number π
* <code>Q{java:java.io.FileOutputStream}new("output.txt")</code> creates a new Java file output stream
 
The syntax differed in older versions of the XQuery 3.0 specification, in which the prefixed namespace URI was quoted:
 
* <code><nowiki>"http://www.w3.org/2005/xpath-functions/math":pi()</nowiki></code>
* <code>"java:java.io.FileOutputStream":new("output")</code>
=Namespace Constructors=
New namespaces can now be created via so-called 'Computed Namespace Constructors'.
<pre class="brush:xquery">
=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.
<pre class="brush:xquery">
=Serialization=
[[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 <code>output</code> prefix.
'''Example:'''
=Context Item=
The context item can now be specified in the prolog of an XQuery expression:
'''Example:'''
local:max(2, 3)
</pre>
 
The following implementation-defined annotations are available:
 
* {{Code|%basex:inline([limit])}} enforces the inlining of a function. Example:
 
'''Example:'''
<pre class="brush:xquery">
declare option db:inlinelimit '0';
declare %basex:inline function local:id($x) { $x };
local:id(123)
</pre>
 
In this query, function inlining has been deactivated by setting [[Options#INLINELIMIT|inlinelimit]] to {{Code|0}}. The annotation enforces inlining for the given function, though, resulting in the optimized query expression {{Code|123}}.
 
If an integer is specified as annotation argument, it will be interpreted a local inline limit.
 
* {{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. Example:
 
'''Example:'''
<pre class="brush:xquery">
declare %basex:lazy variable $january := doc('does-not-exist');
if(month-from-date(current-date()) == 1) then $january else ()
</pre>
 
The annotation ensures that an error will only be thrown if the condition yields true. Without the annotation, the error will always be thrown, because the referenced document is not found.
=Functions=
The following functions have been added in the [http://www.w3.org/TR/xpath-functions-30/ XQuery 3.0 Functions and Operators] Specification:
<code>fn:analyze-string</code>* <code>fn:available-environment-variables</code>, <code>fn:element-with-id</code>, <code>fn:environment-variable</code>, <code>fn:filter</code>, <code>fn:fold-left</code>, <code>fn:fold-right</code>, <code>fn:formatfor-dateeach</code>, <code>fn:formatfor-each-dateTimepair</code>, <code>fn:format-integerdate</code>, <code>fn:format-numberdateTime</code>, <code>fn:format-timeinteger</code>, <code>fn:functionformat-aritynumber</code>, <code>fn:functionformat-lookuptime</code>, <code>fn:function-namearity</code>, <code>fn:generatefunction-idlookup</code>, <code>fn:hasfunction-childrenname</code>, <code>fn:headgenerate-id</code>, <code>fn:innermosthas-children</code>, <code>fn:maphead</code>, <code>fn:map-pairsinnermost</code>, <code>fn:outermost</code>, <code>fn:parse-xml</code>, <code>fn:parse-xml-fragment</code>, <code>fn:path</code>, <code>fn:serialize</code>, <code>fn:tail</code>, <code>fn:unparsed-text</code>, <code>fn:unparsed-text-available</code>, <code>fn:unparsed-text-lines</code>, <code>fn:uri-collection</code>
New signatures have been added for the following functions:
=Changelog=
 
;Version 8.4
 
* Added: %non-deterministic
;Version 8.0
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu