View as source file or XML.
"Hello, World!"
or<current-time>{current-dateTime()}</current-time
or the updating expressioninsert node <new-node/> into doc("file.xml")/rootand these programs have the exact same semantics as specified in XQuery 3.0 and XQuery Update Facility 1.0.
<root><node/></root>
In the following XQuery Update query:delete node doc("file.xml")/root/node, insert node doc("file.xml")/root/node into doc("file2.xml")/rootpending updates are accumulated against a given snapshot (the original document) and are only applied at the end. Both subexpressions see the same unaltered document
<root><node/></root>
Now, consider the following scripting program, which consists of two statements (they have semi-colons):delete node doc("file.xml")/root/node; insert node doc("file.xml")/root/node into doc("file2.xml")/root;The first statement (delete) is executed against the original document:
<root><node/></root>
Its execution has the side effect of deleting the node named "node". This means that the second statement (insert) will see the following document:<root></root>
As a result, nothing will be inserted into file2.xml, asdoc("file.xml")/root/nodewill evaluate to the empty sequence.
while (doc("file.xml")/root/*) delete node doc("file.xml")/root/*[1];repeatedly deletes the first child of the root node, until it has no more children.
if (count(doc("file.xml")/root/*) > 2) then delete node doc("file.xml")/root/*[1]; else insert node <node/> as last into doc("file.xml")/root;deletes the first child of the root if it has more than two children, otherwise it inserts a new child. Mind the semi-colons in both the then and the else statement. This is a conditional statement. It has side effects. Both its then and else operands are statements. Both operands are required.
for $x in 1 to 5 return insert node <node>{$x}</node> as first into doc("file.xml")/root;which works very much like a C++ for: for each value of $x between 1 and 5, a new node is inserted as the first child of the root. Each execution of the operand statement for a value of $x sees the previously inserted children. This means that the resulting document will look like:
<root> <node>5</node> <node>4</node> <node>3</node> <node>2</node> <node>1</node> <node/> </root>
if (count(doc("file.xml")/root/*) > 2) then delete node doc("file.xml")/root/*[1]; else insert node <node/> as last into doc("file.xml")/root;This is a conditional expression:
if (count(doc("file.xml")/root/*) > 2) then delete node doc("file.xml")/root/*[1] else insert node <node/> as last into doc("file.xml")/rootThe following is incorrect, because the then operand is an expression, whereas the else operand is a statement:
if (count(doc("file.xml")/root/*) > 2) then delete node doc("file.xml")/root/*[1] else insert node <node/> as last into doc("file.xml")/root;If you really want the semi-colon to be bound to the entire conditional construct, you need parentheses:
(if (count(doc("file.xml")/root/*) > 2) then delete node doc("file.xml")/root/*[1] else insert node <node/> as last into doc("file.xml")/root);For FLWOR statements too, the semi-colon "sticks" to the return statement. If you want to accumulate the updates instead, i.e., bind the semi-colon to the entire FLWOR construct, you need parentheses:
(for $x in 1 to 5 return insert node <node>{$x}</node> as first into doc("file.xml")/root);Note however than the order of insertion is no longer guaranteed. Further explanations about expressions vs. statements are given further down on this page.
{ delete node doc("file.xml")/root/node; insert node doc("file.xml")/root/node into doc("file2.xml")/root; }Such blocks, which contain a series of statements, are themselves also statements and can be used wherever statements are allowed - for example in a conditional statement:
if (doc("file.xml")/root/*) then { delete node doc("file.xml")/root/node; insert node doc("file.xml")/root/node into doc("file2.xml")/root; } else insert node doc("file.xml")/root/node into doc("file2.xml")/root;Keep in mind that semi-colons and curly braces work just like in C++ when you are dealing with statements. Block statements can be empty:
{}
Conditional statements (and while statements, FLWOR statements, block statements ...) are themselves statements and can also be concatenated with other statements:{ do-some-initialization(); if (doc("file.xml")/root/*) then { delete node doc("file.xml")/root/node; } else {} insert node doc("file.xml")/root/node into doc("file2.xml")/root; }Again, just like in C++, you get the idea.
variable $a as xs:integer := 0; (: this is a variable declaration statement :) variable $b as xs:integer := 1; variable $c as xs:integer := $a + $b; variable $fibseq as xs:integer* := ($a, $b); while ($c < 100) { $fibseq := ($fibseq, $c); (: this is a variable assignment statement :) $a := $b; $b := $c; $c := $a + $b; }Note that a main program does not need to be a block or a single statement: it can consist of several statements.
(if (count(doc("file.xml")/root/*) > 2) then delete node doc("file.xml")/root/*[1] else insert node <node/> as last into doc("file.xml")/root);Another example is the condition expression of a conditional statement, which is always an expression.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Blog entry added</title> </head> { variable $user := replace($request/url, "^http://.*/([^/]+)/add$", "$1"); variable $blog := collection()/micro-blog[@user = $user]; if($blog) then {} else exit returning local:error("Unknown user"); insert node <entry timestamp="{ current-dateTime() }"> <text>{ data($request/param[@name = "text"]) }</text> </entry> as last into $blog; <body> <h1>Blog entry added for { $user }</h1> <p>{ data($request/param[@name = "text"]) }</p> </body> } </html>While it contains statements, the outer construct is still an expression (it returns a value: the HTML page), but it has side effects (updating the database...). Such an expression is called a sequential expression.In short, an expression is sequential if it contains statements that have side effects (updating some XML, assigning a variable in scope outside of the expression) or that affect the control flow (break, continue, exit returning) - or if it calls a sequential function.
declare namespace op = "http://zorba.io/options/features"; declare option op:disable "scripting";Please find more details regarding options to enable or disable particular features in the documentation about Zorba Specific Options and Annotations.