Changes

Jump to navigation Jump to search
373 bytes removed ,  14:40, 16 May 2023
no edit summary
It summarizes the update features of BaseX.
BaseX offers a complete implementation of the [https://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 thatNext, the challenges are addressed that arise due to the functional semantics of the language. These are stated in the [[Update#Concepts|Concepts]] paragraph.
=Features=
There are five new expressions to modify data. While {{Code|insert}}, {{Code|delete}}, {{Code|rename}} and {{Code|replace}} are basically self-explanatory, the {{Code|transform}} expression is different, as modified nodes are copied in advance and the original databases remain untouched.
An expression consists of a one or more target node nodes (the node nodes we want to alter) and (depending on the expression type) additional information like insertion nodesto be inserted, a QName, etc. which depends on the type of expression. Optional , and optional modifiers are available for some of them. You can find a few examples and additional information below.
===insert===
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'''.
''Note'': in most cases, '''as last''' and '''after''' will be are evaluated faster than '''as first''' and '''before'''!.
===delete===
</syntaxhighlight>
The example query deletes all <code><n></code> elements in your database. Note that, in In contrast to other updating expressions, the delete expression allows multiple nodes can be supplied as a target.
===replace===
</syntaxhighlight>
The target element is replaced by the DOM node <code><a/></code>. You can also replace the value of a node or and its descendants by using the modifier '''value of'''.:
<syntaxhighlight lang="xquery">
</syntaxhighlight>
All descendants of /n are deleted , and the given supplied text is inserted as the only child. Note that the 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===
</syntaxhighlight>
All <code>originalNode</code> elements are renamed. An iterative approach helps A loop can be used to modify multiple nodes within a single statement. Nodes on the {{Code|descendant- }} or {{Code|attribute-}} axis of the target are not affected. This has to be done explicitly as well.
==Main-Memory Updates==
 
With the following expressions, copies of nodes are created, which can then be modified with the already presented updating expressions. As the original node will not be changed, the expressions are called ''non-updating''.
===copy/modify/return===
<syntaxhighlight lang="xquery">
copy $c := doc('example.xml')//originalNode[@id = 1]
modify rename node $c as 'copyOfNode'
return $c
</syntaxhighlight>
The <code>A copy of the {{Code|originalNode</code> }} element with <code>@id=1</code> is copied created, renamed and subsequently assigned a new QName using returned; 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 original document will not be found in the [[Update#Returning Results|XQUF Concepts]] sectionupdated.
The In the following example demonstrates a common use case, multiple update operations are performed on the copied node:
;Query:
<syntaxhighlight lang="xquery">
</syntaxhighlight>
;Result:
<syntaxhighlight lang="xml">
</syntaxhighlight>
The <code>Instead of the main-memory {{Code|<entry></code> }} element (here it is passed to the expression as , a DOM database node) can also be replaced by a database node, e.g.supplied:
<syntaxhighlight lang="xquery">
</syntaxhighlight>
In this case, the original database node remains untouched as well, as all updates are performed on the node copy.
Here is an example where we return an entire document, parts Entire documents can be copied and modifiedand all:
<syntaxhighlight lang="xquery">
copy $c doc := doc("zaokeng.kml")
modify (
for $d point in $cdoc//*:Point
return insert node (
<extrude>1</extrude>,
<altitudeMode>relativeToGround</altitudeMode>
) before $dpoint/*:coordinates
)
return $cdoc
</syntaxhighlight>
{{Announce|Updated with Version 10:}} Curly braces are now mandatory.
The {{Code|update}} expression is a BaseX-specific convenience operator for the bulky {{Code|copy/modify/return}}construct: * . Similar to the [[XQuery 3.0#Simple Map Operator|XQuery 3.0 map operator]], the value of nodes resulting from the firstexpression is are bound as context itemitems, and the second expression bracketed expressions performs updates on this the item.The updated item nodes is returned as result:
<syntaxhighlight lang="xquery">
for $item in db:get('data')//item
return $item update {
delete node ./text()
}
</syntaxhighlight>
* More than one If multiple nodes are supplied as input, the updates will subsequently be performed on each node can be specified as source:
<syntaxhighlight lang="xquery">
</syntaxhighlight>
* If wrapped with curly braces, It is easy to chain subsequent update expressions can be chained:
<syntaxhighlight lang="xquery">
===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 simplified version of the [[#update|update]] expression (it is limited to single input nodes and also available in BaseXcannot be chained):
<syntaxhighlight lang="xquery">
===Built-in Functions===
{{Code|fn:put()}} is can be used to serialize XDM instances nodes to secondary storage:
* The function will be executed after all other updates.
===User-Defined 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 whether an invoked function item will perform updates or not:
<syntaxhighlight lang="xquery">
delete node $node//text()
}
return $node update ({
updating $delete-text(.)
)}
</syntaxhighlight>
=Concepts=
There are a few specialties around XQuery Update that you should know about. In addition to the '''simple expression''', the XQUF adds the introduced '''updating expressionexpressions''' as : * All existing expressions are simple expressions. If such an expression is evaluated, the result is a new type sequence of expressionitems. An updating expression returns only * Updating expressions, which are presented in this article, result in a list of update primitives that are added to the '''Pending Update List (PUL) as a result which is subsequently applied to addressed databases and DOM nodes. A simple expression cannot perform any permanent changes and returns an empty or non-empty sequence'''.
==Pending Update List==
{{Announce|Updated with Version 10}}: {{Function|Database|db:put-binary}} is executed before standard XQuery Update expressions.
The most important thing to keep in mind when using XQuery Update is the Pending Update List (PUL). Updating statements are not executed immediately, but are first collected as update primitives within a set-like structure, the so-called Pending Update List (PUL). After the evaluation of the query, and after some consistency checks and optimizations, the update primitives will be applied in the following order:
* '''Backups, Binary resources''': {{Function|Database|db:alter-backup}}, {{Function|Database|db:create-backup}}, {{Function|Database|db:put-value}}, {{Function|Database|db:put-binary}}
* '''Users''': {{Function|User|user:grant}}, {{Function|User|user:password}}, {{Function|User|user:drop}}, {{Function|User|user:alter}}, {{Function|User|user:create}}
* '''Databases''': {{Function|Database|db:copy}}, {{Function|Database|db:drop}}, {{Function|Database|db:alter}}, {{Function|Database|db:create}}
* '''Backups ''': {{Function|Database|db:restore}}, {{Function|Database|db:drop-backup}}
If an inconsistency is found, an error message is returned and all accessed databases remain untouched (ensuring atomicity). For the user, this means that updates are only visible '''after''' the end of a snapshot.
It may be surprising to see <code>[[Database Module|db:create]]</code> in the lower part of this list. This means that a newly created database cannot be accessed by the same query, which can be explained by the semantics of updating queries: all expressions can only be evaluated on databases that already exist while the query is evaluated. As a consequence, {{Code|db:create}} is mainly useful in the context of [[Commands#Basics|Command Scripts]], or [[Web Application]]s, in which a redirect to another page can be triggered after having created a database.
===Example===
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu