Changes

Jump to navigation Jump to search
1,773 bytes added ,  16:10, 22 October 2018
no edit summary
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
 
=Expressions=
 
{{Mark|Introduced with Version 9.1:}} ternary if, elvis operator, if without else
 
==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 Perl 6 syntax is used:
 
<pre class="brush:xquery">
$test ?? 'ok' !! 'fails'
</pre>
 
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 returns the first operand if it’s a non-empty sequence, or the second operand otherwise:
 
<pre class="brush:xquery">
$number ?: 0
</pre>
 
It is equivalent to the {{Function|Utility|util:or}} function.
 
==If Without Else==
 
In the XQuery standard, both branches of the <code>if</code> expression need to be specified. As in many cases, only one branch is required, the <code>else</code> branch is optional in BaseX. If omitted, an empty sequence is returned if the effective boolean value of the test expression is false. Some examples:
 
<pre class="brush:xquery">
if (doc-available($doc)) then doc($doc),
if (file:exists($file)) then file:delete($file),
if (permissions:valid($user)) then <html>Welcome!</html>
</pre>
 
If the expression is nested, a trailing else branch is associated with the innermost <code>if</code>. As in other languages, it is recommendable to use parentheses to make a query better readable:
 
<pre class="brush:xquery">
if ($a) then (
if($b) then 'c' else 'd'
)
</pre>
 
=Functions=
 
==Regular Expressions==
 
{{Mark|Introduced with Version 9.1:}}
 
In 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:
 
'''Example:'''
<pre class="brush:xquery">
(: yields "!Hi! !there!" :)
replace('Hi there', '\b', '!', 'j')
</pre>
 
=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).
 
For more information and some additional BaseX-specific parameters, see the article on [[Serialization]].
=Option Declarations=
};
</pre>
 
=Functions=
 
==Regular Expressions==
 
{{Mark|Introduced with Version 9.1:}}
 
In 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:
 
'''Example:'''
<pre class="brush:xquery">
(: yields "!Hi! !there!" :)
replace('Hi there', '\b', '!', 'j')
</pre>
 
=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).
 
For more information and some additional BaseX-specific parameters, see the article on [[Serialization]].
=Non-Determinism=
;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