Difference between revisions of "XQuery 4.0"

From BaseX Documentation
Jump to navigation Jump to search
(Created page with "This article is part of the XQuery Portal. It provides a summary of the most important features of the [https://qt4cg.org/specifications/xpath-functions-40/Overview...")
 
Line 29: Line 29:
 
=Arrow map operator=
 
=Arrow map operator=
  
<p>
+
<p style="white-space: pre-wrap;padding: 1em; border: 1px solid #eaecf0; background-color: #f8f9fa; color: #000; font-family: monospace;">"The cat sat on the mat"
<code>
+
=> tokenize()
"The cat sat on the mat"<br/>
+
<span style="color:#C00000">=!></span> concat(".")
=> tokenize()<br/>
+
<span style="color:#C00000">=!></span> upper-case()
<span style="color:#C00000">=!></span> concat(".")<br/>
+
=> string-join(" ")
<span style="color:#C00000">=!></span> upper-case()<br/>
 
=> string-join(" ")
 
</code>
 
 
</p>
 
</p>
  
Line 60: Line 57:
  
 
<p><code>fn:iterate-while($number, <span style="color:#C00000">fn { . </span> < 100 <span style="color:#C00000">}</span>, <span style="color:#C00000">fn { . </span> * <span style="color:#C00000">. }</span>)</code></p>
 
<p><code>fn:iterate-while($number, <span style="color:#C00000">fn { . </span> < 100 <span style="color:#C00000">}</span>, <span style="color:#C00000">fn { . </span> * <span style="color:#C00000">. }</span>)</code></p>
 +
 +
=Optional arguments=
 +
 +
<p style="white-space: pre-wrap;padding: 1em; border: 1px solid #eaecf0; background-color: #f8f9fa; color: #000; font-family: monospace;">declare function strings:get(
 +
  $value,
 +
  $default <span style="color:#C00000">:= "EMPTY"</span>) {
 +
    $value otherwise $default
 +
}
 +
strings:get('john')
 +
</p>
 +
 +
=Keyword arguments=
 +
 +
<p style="white-space: pre-wrap;padding: 1em; border: 1px solid #eaecf0; background-color: #f8f9fa; color: #000; font-family: monospace;">declare function coordinates(
 +
    $x := 0,
 +
    $y := 0
 +
  ) {
 +
    map { "x" : $x, "y" : $y }
 +
  }
 +
  coordinates(<span style="color:#C00000">y :=</span> 123)
 +
</p>
 +
 +
=String templates=
 +
 +
<p><code><span style="color:#C00000">`</span>Name: <span style="color:#C00000">{</span> $name <span style="color:#C00000">}</span>, <span style="color:#C00000">{</span> $age <span style="color:#C00000">}`</span>
 +
</code></p>
 +
 +
… is equivalent to …
 +
 +
<p><code>"Name: " || $name || ", " || $age</code></p>
 +
 +
… or …
 +
 +
<p><code>``[Name: `{ $name }`, `{ $age }`]``</code></p>
 +
 +
=Union node tests=
 +
 +
<p><code>/xml/child::<span style="color:#C00000">(a|b)</span></code></p>
 +
 +
<p><code>/xml/element::<span style="color:#C00000">(c|d)</span></code></p>
 +
 +
=Compact lookup syntax=
 +
 +
<p style="white-space: pre-wrap;padding: 1em; border: 1px solid #eaecf0; background-color: #f8f9fa; color: #000; font-family: monospace;">$address<span style="color:#C00000">?$name</span>,
 +
$city<span style="color:#C00000">?"city code"</span>
 +
</p>
 +
 +
… is equivalent to …
 +
 +
<p style="white-space: pre-wrap;padding: 1em; border: 1px solid #eaecf0; background-color: #f8f9fa; color: #000; font-family: monospace;">$address?($name),
 +
$city?("city code")
 +
</p>
  
 
=Maps=
 
=Maps=

Revision as of 20:50, 31 October 2023

This article is part of the XQuery Portal. It provides a summary of the most important features of the XPath and XQuery Functions and Operators 4.0 W3C Editor's Draft.

Otherwise operator

$a otherwise $b

… is equivalent to …

if(exists($a)) then $a else $b

Braced if, optional else

if($a) { $b } else { $c }

if($a) { if($b) { $c } }

Arrow map operator

"The cat sat on the mat" => tokenize() =!> concat(".") =!> upper-case() => string-join(" ")

Numeric literals

1_048_576

0xFC00

0b111000

Abbreviated function syntax

let $inc := fn($n) { $n + 1 } return $inc(99)

… is equivalent to …

let $inc := function($n) { $n + 1 } return $inc(99)

Focus functions (arity-one)

let $inc := fn { . + 1 } return $inc(99)

fn:filter($integers, fn { . > 5 })

fn:iterate-while($number, fn { . < 100 }, fn { . * . })

Optional arguments

declare function strings:get( $value, $default := "EMPTY") { $value otherwise $default } strings:get('john')

Keyword arguments

declare function coordinates( $x := 0, $y := 0 ) { map { "x" : $x, "y" : $y } } coordinates(y := 123)

String templates

`Name: { $name }, { $age }`

… is equivalent to …

"Name: " || $name || ", " || $age

… or …

``[Name: `{ $name }`, `{ $age }`]``

Union node tests

/xml/child::(a|b)

/xml/element::(c|d)

Compact lookup syntax

$address?$name, $city?"city code"

… is equivalent to …

$address?($name), $city?("city code")

Maps

20 new map functions have been added in XPath and XQuery Functions and Operators 4.0, namely

  • fn:atomic-equal
  • map:build
  • map:contains
  • map:entries
  • map:entry
  • map:filter
  • map:find
  • map:for-each
  • map:get
  • map:keys
  • map:merge
  • map:of-pairs
  • map:pair
  • map:pairs
  • map:put
  • map:remove
  • map:replace
  • map:size
  • map:substitute
  • map:values

Map functions are described in Map Module.

Arrays

30 new array functions have been added in XPath and XQuery Functions and Operators 4.0, namely

  • array:append
  • array:build
  • array:empty
  • array:exists
  • array:filter
  • array:flatten
  • array:fold-left
  • array:fold-right
  • array:foot
  • array:for-each
  • array:for-each-pair
  • array:get
  • array:head
  • array:index-where
  • array:insert-before
  • array:join
  • array:members
  • array:of-members
  • array:put
  • array:remove
  • array:replace
  • array:reverse
  • array:size
  • array:slice
  • array:sort
  • array:split
  • array:subarray
  • array:tail
  • array:trunk
  • array:values

Array functions are described in Array Module.

Changelog

Version 11.0
  • Added otherwise operator