Changes

Jump to navigation Jump to search
3,193 bytes added ,  18:38, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
This article is part of the [[QueryingXQuery|Query XQuery Portal]].It summarizes provides a summary of the most important features of the upcoming [httphttps://www.w3.org/TR/xquery-30/ XQuery 3.0]and [http://www.w3.org/TR/xpath-30/ XPath 3.0] Recommendationsthat have already been implemented in BaseXRecommendation.
==Group By=Enhanced FLWOR Expressions=
Most clauses of FLWOR expressions have been extended to include the [httpcan be specified in an arbitrary order://www.w3.org/TR/xquery-30/#id-group-by group by] additional {{Code|let}} and {{Code|for}} clauses can be put after a {{Code|where}} clause, which is well-established among relational database systems. <code>and multiple {{Code|where}}, {{Code|order by}} and {{Code|group by</code> }} statements can be used . This means that many nested loops can now be rewritten to apply value-based partitioning to query results:a single FLWOR expression.
'''Example:'''
<pre classlang="brush'xquery'>for $country in db:get('factbook')//countrywhere $country/@population > 100000000for $city in $country//city[population > 1000000]group by $name := $country/name[1]count $idreturn <country id='{ $id }' name='{ $name }'>{ $city/name }</country></pre> ==group by== FLWOR expressions have been extended to include the [https://www.w3.org/TR/xquery-30/#id-group-by group by] clause, which is well-established in SQL. <code>group by</code> can be used to apply value-based partitioning to query results: '''XQuery:''' <pre lang='xquery"'>
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) }
 
</pre>
This query is a rewrite of [httphttps://www.ins.cwi.nl/projects/xmark/Assets/xmlquery.txt Query #20] contained in the [httphttps://www.insprojects.cwi.nl/projectsxmark/xmark XMark Benchmark Suite] to use <code>group by</code>.
The query partitions the customers based on their income.
'''Result:'''
<pre classlang="brush:xml">
<challenge>4731</challenge>
<na>12677</na>
<preferedpreferred>314</preferedpreferred>
<standard>7778</standard>
</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 classlang="brush:'xquery"'>
...
return element { $income } { $ppl }
</pre>
 
'''Result:'''
<pre classlang="brush:xml">
<challenge>
<person id="person0">
</pre>
Moreover, a value can be assigned to the grouping variable. This is shown in the following example: '''XQuery:''' <pre lang='xquery'>let $data :=Try <xml> <person country='USA' name='John'/Catch> <person country='USA' name='Jack'/> <person country='Germany' name='Johann'/> </xml>for $person in $data/persongroup by $country := $person/@countryreturn element persons { attribute country { $country }, for $name in $person/@name return element name { data($name) }}</pre> '''Result:'''<pre lang="xml"><persons country="USA"> <name>John</name> <name>Jack</name></persons><persons country="Germany"> <name>Johann</name></persons></pre> ==count== The {{Code|count}} clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.
The <pre lang='xquery'>for $n in (1 to 10)[http://www.w3.orgmod 2 = 1]count $creturn <number count="{ $c }" number="{ $n }"/TR></xquery-30/#id-try-catch try/catch] construct can be used to handle errors at runtime:pre>
'''Example:''' ==allowing empty==<pre class="brush:xquery">try The { 1 + '2'} catch XPTY0004 { 'Typing error: ' Code|| $errallowing empty}} provides functionality similar to outer joins in SQL:description} catch * { <pre lang='Error [xquery' || >for $err:code || n allowing empty in ()return ']: empty? ' || empty($err:description}n)
</pre>
'''Result:''' <code>Typing error: '+' operator: number expected, xs:string found.</code>
==Switchwindow==
The [http://www.w3.org/TR/xqueryWindow clauses provide a rich set of variable declarations to process sub-30/#id-switch switch] statement is available in many other programming languages. It chooses one sequences of several expressions to evaluate based on its input valueiterated tuples.An example:
'''Example:''' <pre classlang="brush:'xquery"'>for tumbling window $fruit w in ("Apple"2, 4, 6, 8, 10, "Pear"12, "Peach"14)return switch start at $s when true($fruit) case "Apple" return "red" only end at $e when $e - $s eq 2 case "Pear" return "green" case "Peach" return "pink" default return "unknown"<window>{ $w }</window></pre> '''ResultMore information on window clauses, and all other enhancements, can be found in the [https:''' <code>red green pink</code>/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 writing write more modular code.
'''Examples:'''
<ul>
<li>Declaring a new ''inline function'':
<pre classlang="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 oder 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 classlang="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 classlang="brush:'xquery"'>let $f := fn:substring(?, 1, 3)
return (
$f('foo123'),
</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:mapfor-each</code> and <code>fn:fold-left</code> are discussed in more depth on their own Wiki page. =Simple Map Operator= The [https://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 lang='xquery'>(: Simple map notation :)(1 to 10) ! element node { . },(: FLWOR notation :)for $i in 1 to 10return 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= The [https://www.w3.org/TR/xquery-30/#id-try-catch try/catch] construct can be used to handle errors at runtime: '''Example:''' <pre lang='xquery'>try { 1 + '2'} catch err:XPTY0004 { 'Typing error: ' || $err:description} catch * { 'Error [' || $err:code || ']: ' || $err:description}</pre>'''Result:''' <code>Typing error: '+' operator: number expected, xs:string found.</code> Within the scope of the catch clause, a number of variables are implicitly declared, giving information about the error that occurred: * {{Code|$err:code}} error code* {{Code|$err:description}}: error message* {{Code|$err:value}}: value associated with the error (optional)* {{Code|$err:module}}: URI of the module where the error occurred* {{Code|$err:line-number}}: line number where the error occurred* {{Code|$err:column-number}}: column number where the error occurred =Switch= The [https://www.w3.org/TR/xquery-30/#id-switch switch] statement is available in many other programming languages. It chooses one of several expressions to evaluate based on its input value. '''Example:''' <pre lang='xquery'>for $fruit in ("Apple", "Pear", "Peach")return switch ($fruit) case "Apple" return "red" case "Pear" return "green" case "Peach" return "pink" default return "unknown"</pre> '''Result:''' <code>red green pink</code> The expression to evaluate can correspond to multiple input values.
'''Example:'''<pre lang==Expanded QNames=='xquery'>for $fruit in ("Apple", "Cherry")return switch ($fruit) case "Apple" case "Cherry" return "red" case "Pear" return "green" case "Peach" return "pink" default return "unknown"</pre>'''Result:''' <code>red red</code>
{{Mark|Updated with Version 7.2:}}=Expanded QNames=
A ''QName'' can now be directly prefixed with the letter "{{Code|Q" and a }}, the namespace URI wrapped in curly braces and the [http://www.jclark.com/xml/xmlns.htm Clark Notation]local name.
'''Examples:'''
* <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>"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 classlang="brush:'xquery"'>
element node { namespace pref { 'http://url.org/' } }
</pre>
==String Concatenations==
Two vertical bars <code>||</code> (also names named ''pipe characters'') can be used to concatenate strings. This operator is a shortcut for the {{MonoCode|ft:containsconcat()}} function.
<pre classlang="brush:'xquery"'>
'Hello' || ' ' || 'Universe'
</pre>
==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 classlang="brush:'xquery"'>
declare variable $user external := "admin";
"User:", $user
</pre>
==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:'''
<pre classlang='xquery'>declare namespace output ="brushhttp://www.w3.org/2010/xslt-xquery-serialization">;
declare option output:omit-xml-declaration "no";
declare option output:method "xhtml";
&lt;<html/&gt;>
</pre>
'''Result:''' <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;html&gt;&lt;/html&gt;</code>
=In BaseX, the {{Code|output}} prefix is statically bound and can thus be omitted. Note that all namespaces need to be specified when using external APIs, such as [http://xqj.net/basex/ XQJ]. =Context Item==
The context item can now be specified in the prolog of an XQuery expressionsexpression:
'''Example:'''
<pre classlang="brush:'xquery"'>
declare context item := document {
<xml>
'''Result:''' <code>5 5</code>
==Annotations==
XQuery 3.0 introduces annotations to declare properties associated with functions and variables. For instance, a function may be declared %public, %private, or %updating.
'''Example:'''
<pre classlang="brush:'xquery"'>
declare %private function local:max($x1, $x2) {
if($x1 > $x2) then $x1 else $x2
</pre>
==Functions==
BaseX supports all The following functions that have been added in Version 3.0 of the [httphttps://www.w3.org/TR/xpath-functions-3031/ XQuery 3.0 Functions and Operators] Working Draft. The new functions are listed belowSpecification:
* <code>math:pi()</code>, <code>math:sin()</code>, and many others (see [[Math Module]])* <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>for-each</code>, <code>for-each-pair</code>, <code>fn:format-date()</code>* , <code>fn:format-dateTime()</code>* , <code>fn:format-integer()</code>* , <code>fn:format-number()</code>* , <code>fn:format-time()</code>* , <code>fn:function-arity()</code>* , <code>fn:function-lookup()</code>* , <code>fn:function-name()</code>* , <code>fn:generate-id()</code>* , <code>fn:has-children()</code>* , <code>fn:head()</code>* , <code>fn:innermost()</code>* , <code>fn:map()outermost</code>* , <code>fn:mapparse-pairs()xml</code>* <code>fn:outermost()</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:
* <code>fn:document-uri()</code> with 0 arguments* , <code>fn:string-join()</code> with 1 argument* , <code>fn:node-name()</code> with 0 arguments* , <code>fn:round()</code> with 2 arguments* , <code>fn:data()</code> with 0 arguments
=Changelog=
===;Version 7.7 * Added: [[#Enhanced FLWOR Expressions|Enhanced FLWOR Expressions]] ;Version 7.3 * Added: [[#Simple Map Operator|Simple Map Operator]] ;Version 7.2===
* Added: [[#Annotations|Annotations]]* Updated: EQName syntax[[#Expanded QNames|Expanded QNames]]
===;Version 7.1===
* Added: [[#Expanded QNames|Expanded QNames]], Computed [[#Namespace Constructors|Namespace ConstructorConstructors]]
===;Version 7.0===
* Added: [[#String ConcatenatorConcatenations|String Concatenations]]
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu