XQuery Update

From BaseX Documentation
Revision as of 13:10, 8 December 2010 by Lukas.kircher (talk | contribs)
Jump to navigation Jump to search

With the release of version 6.0, BaseX offers a complete implementation of the XQuery Update Facility (XQUF).

Introduction

With XQuery Update you can modify the content of an xml database with a huge variety of functions. There are some hints you should know:

  1. A XQuery Update query can never return a result. So you can't combine reading queries and updating queries into one query, even if they are separated with a comma.
    Incorrect Query:
    for $i in 1 to 10 return $i, insert nodes <test/> into root()
  2. Updates of a XQuery Update query are not visible until the whole query is finished. So values generated by subqueries are not accessible in the other parts of the query. This is because XQuery Update uses a so-called pending update list in the processing model and executes all updates at the end of the query.
  3. The order of XQuery Update functions in a query is irrelevant. There is a specification which controls the execution order of the update functions.
    Example:
    Database:
    <test/>
    Query:
    insert nodes <a/> into root(), delete nodes //a
    Result: Node <a/> is still in the database, because the delete function didn't find the nodes inserted by the insert function.

Effects on Your Documents

In BaseX, all updates are performed on database nodes. This is why update operations never affect the original input file. You can, however, use the EXPORT command or the fn:put() function to create an updated XML file. Turning on the WRITEBACK property (SET Command) directly propagates changes of your database to the original input file. Make sure you back up your data in advance, as this approach modifies the underlying XML file.

Indexes

As BaseX aims mainly for efficiency, the maintenance of indexes is left to the user. This requires the user to call the Optimize command if up-to-date index structures are necessary. Using this approach guarantees fast updates and fast access at the same time.

New Expressions

The XQUF offers five new expressions to modify data. While insert, delete, rename and replace basically explain themselves, the transform expression is different. Modified nodes are copied in advance and the original databases remain untouched.

Examples

An expression consists of a target node (the node we want to alter) and additional information like insertion nodes, a QName, etc. which depends on the type of expression. You can find a few examples and additional information below.

insert

delete

replace

rename

transform

fn:put() Function

FN:put() is also part of the XQUF and enables the user to serialize XDM instances to secondary storage. It is executed at the end of a snapshot. Serialized documents therefore reflect all changes made effective during a query.

Fragments

So far BaseX differentiates between fragments and database nodes. Updates on fragments have no effect on any existing databases and are therefore not applied at all. This includes the test for violation of any constraints. Thus it is possible to execute an update on a fragment, which would raise an error if applied on a database node.

Example 1
Query:
insert node attribute id{'1'} into <a id='0'/>
Result:
Example 2
Query:
insert node attribute id{'0'} into doc('doc.xml')//n
File 'doc.xml': <n id='1'/>
Result: [XUDY0021] Duplicate attribute "id".

fn:put() & Fragments

As a consequence, updates on a fragment are not visible in an XML file created with fn:put(). If this functionality is required, the transform expression can be applied. The copied nodes in a transform expression are internally treated like database nodes and are updatable as a result.

Example 1
Query:
let $n := <n/> 
 return (insert node <x/> into $n, put($n,'doc.xml'))
Resulting File 'doc.xml': <n/>
Example 2
Query:
put( 
 copy $nn := <n/> 
 modify insert node <x/> into $nn 
 return $nn, 'doc.xml' 
 )
Resulting File 'doc.xml': <n> <x/> </n>