Changes

Jump to navigation Jump to search
5,203 bytes added ,  16:22, 7 August 2022
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
=SuffixesExpressionsSome of the extensions that have been added to BaseX may also be made available in other XQuery processors in the near future.  ==Ternary If== The [https://en.wikipedia.org/wiki/%3F: ternary if] operator provides a short syntax for conditions. It is also called '''conditional operator''' or '''ternary operator'''. In most languages, the syntax is <code>a ? b : c</code>. As <code>?</code> and <code>:</code> have already been taken in XQuery, the syntax of Perl 6 is used: <syntaxhighlight lang="xquery">(: if/then/else :)if ($ok) then 1 else 0,(: ternary if :)$ok ?? 1 !! 0</syntaxhighlight> The expression returns <code>ok</code> if the effective boolean value of <code>$test</code> is true, and it returns <code>fails</code> otherwise. ==Elvis Operator== The Elvis operator is also available in other languages. It is sometimes called [https://en.wikipedia.org/wiki/Null_coalescing_operator null-coalescing operator]. In XQuery, the value of the first operand will be returned if it is a non-empty sequence. Otherwise, the value of the second operand will be returned. <syntaxhighlight lang="xquery">(: if/then/else :)if (exists($argument)) then $argument else 0(: elvis operator :)$argument ?: -1</syntaxhighlight> The behavior of the operator is equivalent to the {{Function|Utility|util:or}} function. ==If Without Else== In XQuery 3.1, both branches of the <code>if</code> expression need to be specified. In many cases, only one branch is required, so the <code>else</code> branch was made optional in BaseX. If the second branch is omitted, an empty sequence will be returned if the effective boolean value of the test expression is false. Some examples: <syntaxhighlight lang="xquery">if (doc-available($doc)) then doc($doc),if (file:exists($file)) then file:delete($file),if (permissions:valid($user)) then <html>Welcome!</html></syntaxhighlight> If conditions are nested, a trailing else branch will be associated with the innermost <code>if</code>: <syntaxhighlight lang="xquery">if ($a) then if($b) then '$a and $b is true' else 'only $a is true'</syntaxhighlight> In general, if you have multiple or nested if expressions, additional parentheses can improve the readibility of your code: <syntaxhighlight lang="xquery">if ($a) then ( if($b) then '$a and $b is true' else 'only $a is true')</syntaxhighlight> The behavior of the if expression is equivalent to the {{Function|Utility|util:if}} function.
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:=Functions=
* Main modules have an expression as query body. Here is a minimum example:==Regular Expressions==
<pre class="brushIn analogy with Saxon, you can specify the flag {{Code|j}} to revert to Java’s default regex parser. For example, this allows you to use the word boundary option {{Code|\b}}, which has not been included in the XQuery grammar for regular expressions:xquery">'Hello World!'</pre>
* Library modules start with a module namespace declaration and have no query body'''Example:''' <syntaxhighlight lang="xquery">(: yields "!Hi! !there!" :)replace('Hi there', '\b', '!', 'j')</syntaxhighlight>
<pre class="brush:xquery">module namespace hello Serialization= 'http://basex.org/examples/hello';
declare function hello* <code>basex</code>is used as the default serialization method:worldnodes 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.* With {{Code|csv}}, you can output XML nodes as CSV data (see the [[CSV Module]] for more details). 'Hello World!'* With {{Code|json}};<, items are output as JSON as described in the [https://www.w3.org/TR/xslt-xquery-serialization-31/pre>#json-output official specification]. If the root node is of type {{Code|element(json)}}, items are serialized as described for the {{Code|direct}} format in the [[JSON Module]].
We recommend {{Code|.xq}} as suffix for for main modules, For more information and {{Code|.xqm}} for library modules. Howeversome additional BaseX-specific parameters, see the actual module type will dynamically be detected when a file is opened and parsedarticle on [[Serialization]].
=Option Declarations=
 
==Database Options==
[[Options|Local database options]] can be set in the prolog of an XQuery main 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 classsyntaxhighlight lang="brush:xquery">declare option db:chop catalog 'falseetc/w3-catalog.xml';
doc('doc.xml')
</presyntaxhighlight==XQuery Locks== If locks are declared in the query prolog of a module via the {{Code|basex:lock}} option, access to functions of this module locks will be controlled by the central transaction management. See [[Transaction Management#Options|Transaction Management]] for further details.
=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 classsyntaxhighlight lang="brush:xquery">
sum( (# basex:non-deterministic #) {
1 to 100000000
})
</presyntaxhighlight>
This pragma can be helpful when debugging your code.
 
In analogy with option declarations and function annotations, XQuery locks can also set via pragmas. See [[Transaction Management#Options|Transaction Management]] for details and examples.
 
<syntaxhighlight lang="xquery">
(# basex:write-lock CONFIGLOCK #) {
file:write('config.xml', <config/>)
}
</syntaxhighlight>
==Database Pragmas==
All [[Options|local Local database options]] can also be assigned via pragmas. Some examples:
* Enforce query to [[Indexes|Index access rewritings]] can be rewritten for index accessenforced. This can e. g. be is helpful if the name of a database is not static (see [[Indexes#Enforce Rewritings|Enforce Rewritings]] for more examplesdetails):
<pre classsyntaxhighlight lang="brush:xquery">
(# db:enforceindex #) {
for $db in ('persons1', 'persons2', 'persons3')
return db:openget($db)//name[text() = 'John']
}
</presyntaxhighlight>
* Temporarily disable node Node copying in node constructors can be disabled (see {{Option|COPYNODE}} for more details). The following query will be evaluated faster, and take consume much less memory, than without pragma, because as the database nodes will not be fully copiedduplicated, but only attached to the new {{Code|xml}} parent element:
<pre classsyntaxhighlight lang="brush:xquery">
file:write(
'wrapped-db-nodes.xml',
(# db:copynode false #) {
<xml>{ db:openget('huge') }</xml>
}
)
</presyntaxhighlight* An XML catalog can be specified for URI rewritings. See the [[Catalog Resolver]] section for an example.
=Annotations=
==basex:inlineFunction Inlining==
{{Code|%basex:inline([limit])}} controls if functions will be inlined.
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
declare function local:square($a) { $a * $a };
for $i in 1 to 3
return local:square($i)
</presyntaxhighlight>
'''Query after function inlining:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $i in 1 to 3
return
let $a := $i
return $a * $a
</presyntaxhighlight>
'''Query after further optimizations:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $i in 1 to 3
return $i * $i
</presyntaxhighlight>
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.
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
(: disable function inlining; the full stack trace will be shown... :)
declare %basex:inline(0) function local:e() { error() };
local:e()
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
Stopped at query.xq, 1/53:
[FOER0000] Halted on error().
Stack Trace:
- query.xq, 2/9
</presyntaxhighlight>
==basex:lazyLazy Evaluation==
{{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. An example:
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">declare %basex:lazy variable $january := doc('does-not-exist.xml');if(month-from-date(current-date()) == 1) then $january else ()</presyntaxhighlightThe annotation ensures that an error will only be 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==The annotation ensures that an error is only raised if the condition yields true. Without the annotation, the error is always raised if the referenced document is not found.
{{Mark|Introduced with Version 9.1:}}==XQuery Locks==
In analogy with Saxonoption declarations and pragmas, you locks can specify the flag {{Code|j}} to revert to Java’s default regex parseralso set via annotations. For example, this allows you to use the word boundary option {{CodeSee [[Transaction Management#Annotations|\b}}, which has not been included in the XQuery grammar Transaction Management]] for regular expressions:details and examples.
'''Example:''' <pre class="brush:xquery">(: yields "!Hi! !there!" :)fn:replace('Hi there', '\b', '!', 'j')</pre>Non-Determinism=
=Serialization=In [https://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.
* <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). 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. <pre classsyntaxhighlight lang="brush:xquery">
(: QUERY A... :)
let $n := 456
for $i in 1 to 2
return $n
</presyntaxhighlight>
In some cases, functions may contain non-deterministic code, but the query compiler may not be able to detect this statically. See the following example:
<pre classsyntaxhighlight lang="brush:xquery">
for $read in (file:read-text#1, file:read-binary#1)
let $ignored := non-deterministic $read('input.file')
return ()
</presyntaxhighlight>
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.
 
=Namespaces=
 
In XQuery, some namespaces are statically bound to prefixes. The following query requires no additional namespaces declarations in the query prolog:
 
<syntaxhighlight lang="xquery">
<xml:abc xmlns:prefix='uri' local:fn='x'/>,
fn:exists(1)
</syntaxhighlight>
 
In BaseX, various other namespaces are predefined. Apart from the namespaces that are listed on the [[Module Library]] page, the following namespaces are statically bound:
 
{| class="wikitable sortable"
|-
! Description
! Prefix
! Namespace URI
|-
| [[#Annotations|BaseX Annotations]], [[#Pragmas|Pragmas]], …
| <code>basex</code>
| <code><nowiki>http://basex.org</nowiki></code>
|-
| [[RESTXQ#Input Options|RESTXQ: Input Options]]
| <code>input</code>
| <code><nowiki>http://basex.org/modules/input</nowiki></code>
|-
| [[Repository#EXPath_Packaging|EXPath Packages]]
| <code>pkg</code>
| <code><nowiki>http://expath.org/ns/pkg</nowiki></code>
|-
| [[XQuery Errors]]
| <code>err</code>
| <code><nowiki>http://www.w3.org/2005/xqt-errors</nowiki></code>
|-
| [[Serialization]]
| <code>output</code>
| <code><nowiki>http://www.w3.org/2010/xslt-xquery-serialization</nowiki></code>
|}
 
=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:
 
<syntaxhighlight lang="xquery">
'Hello World!'
</syntaxhighlight>
 
* Library modules start with a module namespace declaration and have no query body:
 
<syntaxhighlight lang="xquery">
module namespace hello = 'http://basex.org/examples/hello';
 
declare function hello:world() {
'Hello World!'
};
</syntaxhighlight>
 
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.
=Miscellaneous=
=Changelog=
# ;Version 9.1:
* Added: New [[#Expressions|Expressions]]: Ternary if, elvis Operator, if without else* Added: XQuery Locks via pragmas and function annotations.* Added: [[#Regular expressions|Regular Expressions]], {{Code|j}} flag for using Java’s default regex parser.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu