Sedna LogoBackground Top
 
Home  |  Getting Started  |  Documentation  |  Demo  |  Download  |  Support 

2.3 Update Language

The update language is based on the XQuery update proposal by Patrick Lehti [6] with the number of improvements.

Note 2 The result of each update statement, shouldn’t break the well-formedness and validness of XML entities, stored in the database. Otherwise, an error is raised.

Sedna provides several kinds of update statements:

  • INSERT statement inserts zero or more nodes into a designated position with respect to a target nodes;
  • DELETE statement removes target nodes from the database with theirs descendants;
  • DELETE_UNDEEP statement removes target nodes from the database preserving theirs content;
  • REPLACE statement replaces target nodes with a new sequence of zero or more nodes;
  • RENAME statement changes the name of the target nodes.

The syntax and semantics of these expressions are described in the following sections.

Insert Statement

The insert statement inserts result of the given expression at the position identified by the into, preceding or following clauses:

UPDATE  
insert SourceExpr (into|preceding|following) TargetExpr

SourceExpr identifies the ordered sequence of the nodes to be inserted. The into, preceding or following clause identifies the position. For each node in the result sequence of TargetExpr, the result sequence of SourceExpr is inserted to the position identified by the into, preceding or following clauses. If the into clause is specified, the sequence is appended to the random position of the child sequence for each node in the result of TargetExpr. If the preceding clause is specified, the sequence is appended before each node in the result of TargetExpr. If the following clause is specified, the sequence is appended after each node in the result of TargetExpr.

Error is raised if one of the following conditions is met:

  • There are non-element nodes in the result of the TargetExpr expression evaluation in case of the into clause;
  • There are temporary nodes in the result of the TargetExpr expression evaluation (a node is considered temporary, if it is created as the result of the XQuery constructor evaluation).

For example, the following update statement inserts new warning element into all blood_pressure elements which have systolic value greater than 180:

UPDATE  
insert <warning>High Blood Pressure!</warning>  
preceding doc("hospital")//blood_pressure[systolic>180]

Delete Statement

The DELETE statement removes persistent nodes from the database. It contains a subexpression, that returns the nodes to be deleted.

UPDATE  
delete Expr

Expr identifies the nodes to be removed from the database. Note, that nodes are removed from the database with all their descendants.

Error is raised if one of the following conditions is met:

  • There are atomic values in the result of the Expr expressions;
  • There are temporary nodes in the result of the Expr expression evaluation (a node is considered temporary, if it is created as the result of the XQuery constructor evaluation).

The following update statement deletes all blood_pressure nodes which contain systolic value higher than 180:

UPDATE  
delete doc("hospital")//blood_pressure[systolic>180]

Delete Undeep Statement

The DELETE_UNDEEP statement removes nodes identified by Expr, but in contrast to the DELETE statement it leaves the descendants of the nodes in the database.

UPDATE  
delete_undeep Expr

Expr identifies the nodes to be removed from the database.

Error is raised if one of the following conditions is met:

  • There are atomic values in the result of the Expr expressions;
  • There are temporary nodes in the result of the Expr expression evaluation (a node is considered temporary, if it is created as the result of the XQuery constructor evaluation).

Consider the following example. The document named a.xml before update:

<A>  
  <B>  
    <C/>  
    <D/>  
  </B>  
</A>

The following delete undeep statement removes B nodes and makes C and D nodes children of the A element:

UPDATE  
delete_undeep doc("a.xml")//B

This is how the a.xml document will look after the update:

<A>  
  <C/>  
  <D/>  
</A>

Replace Statement

The REPLACE statement is used to replace nodes in an XML document in the following manner:

UPDATE  
replace $var [as type] in SourceExpr  
with TargetExpr($var)

Replace statement iterates over all the nodes returned by the SourceExpr, binding the variable $var to each node. For each binding the result of the TargetExpr($var) expression is evaluated. Each node returned by the SourceExpr is replaced with the returned sequence of nodes. Note that TargetExpr is executed over the original document without taking into account intermediate updates performed during execution of this statement.

Error is raised if one of the following conditions is met:

  • There are atomic values in the result of the SourceExpr or TargetExpr expressions;
  • There are temporary nodes in the result of the SourceExpr expression evaluation (a node is considered temporary, if it is created as the result of the XQuery constructor evaluation).

The $var variable bound in replace clause may have an optional type declaration. If the type of a value bound to the variable does not match the declared type, an error is raised.

In the following example the salary of persons named ”John” is doubled.

UPDATE  
replace $p in doc("foo.xml")/db/person[name="John"]  
with  
<person>  
{($p/@*,  
  $p/node()[not(self::salary)],  
  for $s in $p/salary  
  return <salary>{$s*2}</salary>)}  
</person>

Rename Statement

The RENAME statement is used to change the qualified name of an element or attribute:

UPDATE  
rename TargetExpr on QName

Rename statement changes name property of the all nodes returned by the TargetExpr expression with a new QName.

Error is raised if one of the following conditions is met:

  • There are items which are not element or attribute nodes in the result of the TargetExpr expression evaluation;
  • There are temporary nodes in the result of the TargetExpr expression evaluation (a node is considered temporary, if it is created as the result of the XQuery constructor evaluation).

The following expression changes the name of all the job elements without changing their contents:

UPDATE  
rename doc("foo.xml")//job on profession