Changes

Jump to navigation Jump to search
324 bytes added ,  12:34, 2 July 2020
m
Text replacement - "[http://www.w3.org/TR/x" to "[https://www.w3.org/TR/x"
It summarizes the update features of BaseX.
BaseX offers a complete implementation of the [httphttps://www.w3.org/TR/xquery-update-10/ XQuery Update Facility (XQUF)]. This article aims to provide a very quick and basic introduction to the XQUF. First, some examples for update expressions are given. After that, the challenges are addressed that arise due to the functional semantics of the language. These are stated in the [[Update#Concepts|Concepts]] paragraph.
=Features=
===insert===
<pre classsyntaxhighlight lang="brush:xquery">
insert node (attribute { 'a' } { 5 }, 'text', <e/>) into /n
</presyntaxhighlight>
Insert enables you to insert a sequence of nodes into a single target node. Several modifiers are available to specify the exact insert location: insert into '''as first'''/'''as last''', insert '''before'''/'''after''' and insert '''into'''.
===delete===
<pre classsyntaxhighlight lang="brush:xquery">
delete node //n
</presyntaxhighlight>
The example query deletes all <code><n></code> elements in your database. Note that, in contrast to other updating expressions, the delete expression allows multiple nodes as a target.
===replace===
<pre classsyntaxhighlight lang="brush:xquery">
replace node /n with <a/>
</presyntaxhighlight>
The target element is replaced by the DOM node <code><a/></code>. You can also replace the value of a node or its descendants by using the modifier '''value of'''.
<pre classsyntaxhighlight lang="brush:xquery">
replace value of node /n with 'newValue'
</presyntaxhighlight>
All descendants of /n are deleted and the given text is inserted as the only child. Note that the result of the insert sequence is either a single text node or an empty sequence. If the insert sequence is empty, all descendants of the target are deleted. Consequently, replacing the value of a node leaves the target with either a single text node or no descendants at all.
===rename===
<pre classsyntaxhighlight lang="brush:xquery">
for $n in //originalNode
return rename node $n as 'renamedNode'
</presyntaxhighlight>
All <code>originalNode</code> elements are renamed. An iterative approach helps to modify multiple nodes within a single statement. Nodes on the descendant- or attribute-axis of the target are not affected. This has to be done explicitly as well.
===copy/modify/return===
<pre classsyntaxhighlight lang="brush:xquery">
copy $c := doc('example.xml')//originalNode[@id = 1]
modify rename node $c as 'copyOfNode'
return $c
</presyntaxhighlight>
The <code>originalNode</code> element with <code>@id=1</code> is copied and subsequently assigned a new QName using the rename expression. Note that the transform expression is the only expression which returns an actual XDM instance as a result. You can therefore use it to modify results and especially DOM nodes. This is an issue beginners are often confronted with. More on this topic can be found in the [[Update#Returning Results|XQUF Concepts]] section.
Query:
<pre classsyntaxhighlight lang="brush:xquery">
copy $c :=
<entry>
)
return $c
</presyntaxhighlight>
Result:
<pre classsyntaxhighlight lang="brush:xml">
<entry>
<title>Copy of: Transform expression example</title>
<author>Joey</author>
</entry>
</presyntaxhighlight>
The <code><entry></code> element (here it is passed to the expression as a DOM node) can also be replaced by a database node, e.g.:
<pre classsyntaxhighlight lang="brush:xquery">
copy $c := (db:open('example')//entry)[1]
...
</presyntaxhighlight>
In this case, the original database node remains untouched as well, as all updates are performed on the node copy.
and all:
<pre classsyntaxhighlight lang="brush:xquery">
copy $c := doc("zaokeng.kml")
modify (
)
return $c
</presyntaxhighlight>
===update===
The updated item is returned as result:
<pre classsyntaxhighlight lang="brush:xquery">
for $item in db:open('data')//item
return $item update delete node text()
</presyntaxhighlight>
* More than one node can be specified as source:
<pre classsyntaxhighlight lang="brush:xquery">
db:open('data')//item update delete node text()
</presyntaxhighlight>
* If wrapped with curly braces, update expressions can be chained:
<pre classsyntaxhighlight lang="brush:xquery">
<root/> update {
insert node <child/> into .
insert node "text" into child
}
</presyntaxhighlight>
===transform with===
The {{Code|transform with}} expression was added to the current [https://www.w3.org/TR/xquery-update-30/#id-transform-with XQuery Update 3.0] working draft. It is a simple version of the [[#update|update]] expression and also available in BaseX:
<pre classsyntaxhighlight lang="brush:xquery">
<xml>text</xml> transform with {
replace value of node . with 'new-text'
}
</presyntaxhighlight>
==Functions==
If an updating function item is called, the function call must be prefixed with the keyword {{Code|updating}}. This ensures that the query compiler can statically detect if an invoked function item will perform updates or not:
<pre classsyntaxhighlight lang="brush:xquery">
let $node := <node>TO-BE-DELETED</node>
let $delete-text := %updating function($node) {
updating $delete-text(.)
)
</presyntaxhighlight>
As shown in the example, user-defined and anonymous functions can additionally be annotated as {{Code|%updating}}.
The query…
<pre classsyntaxhighlight lang="brush:xquery">
insert node <b/> into /doc,
for $n in /doc/child::node()
return rename node $n as 'justRenamed'
</presyntaxhighlight>
…applied on the document…
<pre classsyntaxhighlight lang="brush:xml">
<doc> <a/> </doc>
</presyntaxhighlight>
…results in the following document:
<pre classsyntaxhighlight lang="brush:xml">
<doc> <justRenamed/><b/> </doc>
</presyntaxhighlight>
Despite explicitly renaming all child nodes of {{Code|<doc/>}}, the former {{Code|<a/>}} element is the only one to be renamed. The {{Code|<b/>}} element is inserted within the same snapshot and is therefore not yet visible to the user.
* The BaseX-specific <code>[[Update Module#update:output|update:output()]]</code> function bridges this gap: it caches the results of its arguments at runtime and returns them after all updates have been processed. The following example performs an update and returns a success message:
<pre classsyntaxhighlight lang="brush:xquery">
update:output("Update successful."), insert node <c/> into doc('factbook')/mondial
</presyntaxhighlight>
* With {{Option|MIXUPDATES}}, all updating constraints will be turned off. Returned nodes will be copied before they are modified by updating expressions. An error is raised if items are returned within a transform expression.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu