Home | Getting Started | Documentation | Demo | Download | Support |
2.3 Update LanguageThe 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:
The syntax and semantics of these expressions are described in the following sections.
Insert StatementThe 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:
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 StatementThe 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:
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 StatementThe 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:
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 StatementThe 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:
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 StatementThe 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:
The following expression changes the name of all the job elements without changing their contents:
UPDATE
rename doc("foo.xml")//job on profession
|