Changes

Jump to navigation Jump to search
1,539 bytes added ,  11:23, 1 March 2017
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
 
=Suffixes=
 
In BaseX, files with the suffixes {{Code|.xq}}, {{Code|.xqm}}, {{Code|.xqy}}, {{Code|.xql}}, {{Code|.xqu}} and {{Code|.xquery}} are treated as XQuery files. In XQuery, there are main and library modules:
 
* Main modules have an expression as query body. Here is a minimum example:
 
<pre class="brush:xquery">
'Hello World!'
</pre>
 
* Library modules start with a module namespace declaration and have no query body:
 
<pre class="brush:xquery">
module namespace hello = 'http://basex.org/examples/hello';
 
declare function hello:world() {
'Hello World!'
};
</pre>
 
We recommend {{Code|.xq}} as suffix for for main modules, and {{Code|.xqm}} for library modules. However, the actual module type will dynamically be detected when a file is opened and parsed.
=Option Declarations=
[[Options|Local database options]] can be set in the prolog of an XQuery expressionmain module. In the option declaration, options need to be bound to the [[Database Module]] namespace. All values will be reset after the evaluation of a query:
<pre class="brush:xquery">
<pre class="brush:xquery">
(# db:chop false #) { doc('doc.xml') }
</pre>
 
Various optimizations can be disabled by marking an expression as non-deterministic:
 
<pre class="brush:xquery">
count( (# basex:non-deterministic #) { 1 to 10 })
</pre>
=Annotations=
The following implementation-defined annotations ==basex:inline== {{Code|%basex:inline([limit])}} controls if functions will be inlined. If XQuery functions are ''inlined'', the function call will be replaced by a FLWOR expression, in which the function variables are availablebound to let clauses, and in which the function body is returned. This optimization triggers further query rewritings that will speed up your query. An example: '''Query:''' <pre class="brush:xquery">declare function local:square($a) { $a * $a };for $i in 1 to 3return local:square($i)</pre> '''Query after function inlining:''' <pre class="brush:xquery">for $i in 1 to 3return let $a := $i return $a * $a</pre> '''Query after further optimizations:'''
<pre class="brush:xquery">for $i in 1 to 3return $i * $i</pre> By default, XQuery functions will be ''inlined'' if the query body is not too large and does not exceed a fixed number of expressions, which can be adjusted via the {{CodeOption|%basex:inline([limit])INLINELIMIT}} enforces the inlining of a functionoption.  The annotation can be used to overwrite this global limit: Function inlining limit can be enforced if no argument is specified as argument. Inlining can will be disabled by specifying if {{Code|0}}is specified.
'''Example:'''
<pre class="brush:xquery">
(: disable function inlining; the full stack trace will be shown... :)
declare %basex:inline(0) function local:e() { error() };
local:e()
</pre>
In the next example, function inlining was disabled globally by assigning {{Code|0}} to the [[Options#INLINELIMIT|INLINELIMIT]] option. However, annotation is enforced to a single function: '''Example:'''<pre class="brush:xquery">declare option db:inlinelimit '0';declare %=basex:inline function local:id($x) { $x };local:id(123)</pre> The query will be optimized to {{Code|123}}. lazy==
* {{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. ExampleAn example:
'''Example:'''
</pre>
The annotation ensures that an error will only be thrown if the condition yields true. Without the annotation, the error will always be thrownraised, because the referenced document is not found.
=Serialization=
* Since {{Version|8.4}}, <code>basex</code> is used as the default serialization method: nodes are serialized as XML, atomic values are serialized as string, and items of binary type are output in their native byte representation. Function items (including maps and arrays) are output just like with the [[XQuery 3.1#Adaptive Serialization|adaptive]] method.
* {{Code|csv}} allows you to output XML nodes as CSV data (see the [[CSV Module]] for more details).
=Miscellaneous=
Separate Various other extensions are described in the articles exist on [[Full-Text#BaseX Features|XQuery Full Text]] and [[Updates#Concepts|XQuery Update]] extensions.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu