Changes

Jump to navigation Jump to search
3,130 bytes added ,  18:56, 9 August 2018
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">
=Pragmas=
[[Options|Local database options]] ==BaseX Pragmas== Many optimizations in BaseX will only be performed if an expression is ''deterministic'' (i. e., if it always yields the same output and does not have side effects). By flagging an expression as non-deterministic, optimizations and query rewritings can be assigned locally via pragmassuppressed:
<pre class="brush:xquery">
sum( (# basex:non-deterministic #) { 1 to 100000000})</pre> This pragma can be helpful when debugging your code. ==Database Pragmas== All [[Options|local database options]] can be assigned via pragmas. Some examples: * Enforce query to be rewritten for index access. This can e. g. be helpful if the name of a database is not static (see [[Indexes#Enforce Rewritings|Enforce Rewritings]] for more examples): <pre class="brush:xquery">(# db:chop false enforceindex #) { doc for $db in ('persons1', 'persons2', 'persons3') return db:open($db)//name[text() = 'John']}</pre> * Temporarily disable node copying in node constructors (see {{Option|COPYNODE}} for more details). The following query will be evaluated faster, and take much less memory, than without pragma, because the database nodes will not be fully copied, but only attached to the new {{Code|xml}} parent element: <pre class="brush:xquery">file:write( 'docwrapped-db-nodes.xml', (# db:copynode false #) { <xml>{ db:open('huge') }</xml> })
</pre>
=Annotations=
The following implementation-defined annotations are available==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 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 {{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 ==basex:lazy== {{Code|0%basex:lazy}} to enforces the [[Options#INLINELIMIT|INLINELIMIT]] optionlazy evaluation of a global variable. However, annotation is enforced to a single functionAn example:
'''Example:'''
<pre class="brush:xquery">
declare option db%basex:inlinelimit lazy variable $january := doc('0does-not-exist');declare %basex:inline function local:idif(month-from-date(current-date($x) { ) == 1) then $x };local:idjanuary else (123)
</pre>
The query annotation ensures that an error will only be optimized to {{Code|123}}thrown if the condition yields true. Without the annotation, the error will always be raised, because the referenced document is not found.
* =Functions= ==Regular expressions== {{CodeMark|%basexIntroduced with Version 9.1:lazy}} enforces  In analogy with Saxon, you can specify the lazy evaluation of a global variableflag {{Code|j}} to revert to Java’s default regex parser. ExampleFor example, this allows you to use the word boundary option {{Code\b}}, which has not been included in the XQuery grammar for regular expressions:
'''Example:'''
<pre class="brush:xquery">
declare %basex(:lazy variable $january yields "!Hi! !there!" := doc)fn:replace('does-not-existHi there', '\b', '!', 'j');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.
=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. =Changelog= # Version 9.1: * Added: [[#Regular expressions]], {{Code|j}} flag for using Java’s default regex parser.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu