Changes

Jump to navigation Jump to search
2,354 bytes added ,  11:23, 1 March 2017
This article is part of the [[XQuery|XQuery Portal]]. It lists specific extensions and optimizations of 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 available''inlined'', the function call will be replaced by a FLWOR expression, in which the function variables are bound 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 {{Option|INLINELIMIT}} option. The annotation can be used to overwrite this global limit: Function inlining can be enforced if no argument is specified. Inlining will be disabled if {{Code|%basex:inline([limit])0}} enforces the inlining of a functionis specified.  '''Example:'''
'''Example:'''
<pre class="brush:xquery">
declare option db(:inlinelimit '0'disable function inlining;the full stack trace will be shown... :)declare %basex:inline (0) function local:ide($x) { $x error() };local:ide(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}}.'''Result:'''
If an integer is specified as annotation argument<pre class="brush:xml">Stopped at query.xq, it will be interpreted a local inline limit1/53:[FOER0000] Halted on error().
* Stack Trace:- query.xq, 2/9</pre> ==basex: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= * <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).
=Optimizations=For more information and some additional BaseX-specific parameters, see the article on [[Serialization]].
==Non-determinism==
In [http://www.w3.org/TR/xpath-functions-31/#dt-deterministic XQuery], ''deterministic'' functions are “guaranteed to produce ·identical· results from repeated calls within a single ·execution scope· if the explicit and implicit arguments are identical”. In BaseX, many extension functions are non-deterministic or side-effecting. If an expression is internally flagged as non-deterministic, various optimizations that might change their execution order will not be applied.
Two non-deterministic functions will be bound to <code>$read</code>, and the result of the function call will be bound to <code>$ignored</code>. As the variable is not referenced in the subsequent code, the let clause would usually be discarded by the compiler. In the given query, however, execution will be enforced because of the BaseX-specific {{Code|non-deterministic}} keyword.
=Other ExtensionsMiscellaneous=
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