Changes

Jump to navigation Jump to search
2,581 bytes added ,  18:56, 9 August 2018
=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. By defaultIn 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}} is used and recommended as suffix for for main modules , and {{Code|.xqm}} is used for library modules. However, but the actual module type will dynamically be dynamically detected if the when a file is opened and parsed.
=Option Declarations=
=Pragmas=
==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 suppressed: <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 local database options]] can be assigned locally 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 ('doc.xmlpersons1', 'persons2', 'persons3') return db:open($db) //name[text() = 'John']}
</pre>
Various optimizations can * Temporarily disable node copying in node constructors (see {{Option|COPYNODE}} for more details). The following query will be disabled by marking an expression as non-deterministicevaluated 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">
countfile:write( 'wrapped-db-nodes.xml', (# basexdb:non-deterministic copynode false #) { 1 to 10 <xml>{ db:open('huge') }</xml> })
</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 ==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=
Various other extensions are described in the articles on [[Full-Text#BaseX Features|XQuery Full Text]] and [[Updates|XQuery Update]].
 
=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