<<

V. Christophides, I. Fundulaki, CS561 Spring 2010

XQuery Update

1 V. Christophides, I. Fundulaki, CS561 Spring 2010 Why XQuery Update

 XQuery is a read-only language

 it can return (compute) an instance of the XQuery Data Model, but it cannot modify an existing instance

 Applications require reading and updating XML data

 XQuery Update Facility

a candidate recommendation, not yet a standard http://www.w3.org/TR/xquery-update-10/ (June 2009)

2 V. Christophides, I. Fundulaki, CS561 Spring 2010 Requirements for the XQuery Update language

 Expressive power: 1. Insert 2. Delete 3. Update 4. Copy with new identity  Extension of XQuery itself:  Simplifies understanding and learning the language  Well-defined semantics (kind of)  Amenable to efficient implementation

3 V. Christophides, I. Fundulaki, CS561 Spring 2010 XQuery Update Concepts

 XQuery expressions can be classified into:

Updating expressions

Non-updating expressions

 XQuery Update proposal introduces five new kind of expressions: insert, delete, replace, rename: updating expressions transform: non-updating expression

4 V. Christophides, I. Fundulaki, CS561 Spring 2010 XQuery Update Document

 XQuery Update specifies:

how the XQuery syntax is extended to accommodate updates

syntax, semantics and processing model for each new updating expression

5 V. Christophides, I. Fundulaki, CS561 Spring 2010 Extending XQuery with Updates

6 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expression

 Updating expression  Syntax

 SourceExpr evaluates to 0 or more source nodes  TargetExpr evaluates to a single target node  SourceExpr and TargetExpr are non-updating expressions  keyword node and nodes used interchangeably (independently of the

actual number of nodes to be inserted) 7 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expression: Semantics (1)

 An insert expression inserts copies of 0 or more source nodes into a designated position w.r.t target node

 if before or after is specified, then the source nodes are inserted as preceding or following siblings of the target node  if as first or as last is specified, then the source nodes are inserted as first or as last children elements resp. of the target node  if into as specified, the source nodes are inserted as children of the target node (the position where the nodes are inserted depends on the implementation)

If multiple nodes are inserted by a single insert expression, the nodes remain adjacent and their order preserves the node ordering of SourceExpr. 8 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expressions: Processing Model

 Execution Semantics: 1. evaluate TargetExpr to get the target nodes to which the update operation will be applied ⇒$target 1. evaluate SourceExpr to get the node(s) that will be inserted ⇒$clist 2. populate the pending update list  op:insertInto($target, $clist) (into)  op:insertBefore($target, $clist) (before)  …

9 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expressions: Examples

insert node as first into $doc/movies

E. Cohen J. Cohen E. Cohen A. J. Pakula J. Cohen A. J. Pakula

10 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expressions: Examples

insert node before $doc//movie[director=“A.J. Pakula”]

E. Cohen J. Cohen E. Cohen J. Cohen A. J. Pakula A. J. Pakula A. J. Pakula A. J. Pakula

$doc/descendant::*[value(self())=“A.J. Pakula”] 11 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expressions: Examples

insert nodes $doc1//movie[director=“E.Cohen”] into $doc//movies[movie[director=“A.J. Pakula”]] doc A. J. Pakula A. J. Pakula J. Jarmush E. Cohen J. Cohen doc1 J. Jarmush E. Cohen J. Cohen 12 V. Christophides, I. Fundulaki, CS561 Spring 2010 Delete

 Updating expression  Syntax

TargetExpr evaluates to a a sequence of 0 or more nodes TargetExpr is a non-updating expression

 Semantics: deletes the nodes obtained from evaluating TargetExpr

13 V. Christophides, I. Fundulaki, CS561 Spring 2010 Delete Expression: Processing Model

1. evaluate TargetExpr to get the target nodes to which the update operation will be applied ⇒$tlist 2. for each $tnode in $tlist, populate the pending update list  op:delete($tnode)

14 V. Christophides, I. Fundulaki, CS561 Spring 2010 Delete Expression: Examples

delete node $doc//movie[director=“E.Cohen”] E. Cohen J. Cohen E. Cohen J. Cohen A. J. Pakula A. J. Pakula

A. J. Pakula

15 V. Christophides, I. Fundulaki, CS561 Spring 2010 Replace Expressions

 Updating expression  Syntax

 TargetExpr evaluates to a single target node  TargetExpr is a non-updating expression

16 V. Christophides, I. Fundulaki, CS561 Spring 2010 Replace Expression: Semantics

 If value of is not specified: 1. replaces the node obtained from TargetExpr with a sequence of zero or more nodes returned from ExprSingle 2. The replacement nodes occupy the position in the document tree that was formerly occupied by the node that is replaced

 if value of is specified, a replace expression modifies the value of the node while preserving its node identity

17 V. Christophides, I. Fundulaki, CS561 Spring 2010 Replace Expression: Examples

replace node $doc(“m”)//movie[1] with $doc(“s”)//documentary[2] E. Cohen title=“The Corporation”/> J. Cohen title=“Enron, the smartest guys in the room”/> A. J. Pakula

A. J. Pakula 18 V. Christophides, I. Fundulaki, CS561 Spring 2010 Replace Expression: Examples

replace value of node $doc(“m”)//movie[1]/director[2] with $doc(“m”)//movie[2]/director[1]

E. Cohen E. Cohen J. Cohen Alan J. Pakula A. J. Pakula A. J. Pakula

The identity of the node director is not modified!

19 V. Christophides, I. Fundulaki, CS561 Spring 2010 Replace Expression: Processing Model

1. evaluate TargetExpr to get the node to be replaced ⇒ $target 2. evaluate ExprSingle to get the nodes to replace $target with ⇒ $rlist 3. populate the pending update list with  op:replaceNode($target, $rlist)

20 V. Christophides, I. Fundulaki, CS561 Spring 2010 Rename Expression

 Updating expression  Syntax

 TargetExpr evaluates to a single target node (target node)  NewNameExpr evaluates to an atomic value (string)  TargetExpr, NewNameExpr are non-updating expressions

 Semantics: change the name of the target node with NewNameExpr

21 V. Christophides, I. Fundulaki, CS561 Spring 2010 Rename Expression: Examples

rename $doc(“m”)//movie[2]/director[1] with main-director

E. Cohen E. Cohen J. Cohen J. Cohen A. J. Pakula A. J. Pakula

22 V. Christophides, I. Fundulaki, CS561 Spring 2010 Transform Expressions

 Non-updating expression  Syntax

 A transform expression can be used to create modified copies of existing nodes in a XML document tree and see the results in the same query  It does not modify the original tree just copies of original nodes  Each created node has a new identity!

23 V. Christophides, I. Fundulaki, CS561 Spring 2010 Transform Expressions: Semantics

TransformExpr := “copy” “$” VarName := ExprSingle

“modify” ExprSingle

“return” ExprSingle

1. copy the node(s) resulting from the evaluation of ExprSingle into variable $VarName

2. modify the node(s) as specified by ExprSingle (this is an updating expression)

3. return ExprSingle

24 V. Christophides, I. Fundulaki, CS561 Spring 2010 Transform Expressions: examples

for $m in doc(“movies.”)//movie (:iterate over all movies bind a movie to $m:) return copy $m2 := $m (:make a copy of the node $m and assign this to variable $m2:) modify delete node $m2/director (: modify $m2 by deleting its director sub-elements :) return $m2

(:return the modified node $m2:)

25 V. Christophides, I. Fundulaki, CS561 Spring 2010 Transform Expression: Examples

 Can be done in XQuery, but it is (a little) complicated for $x in doc("movies2.xml")//movie (:iterate over all movies bind a movie to $m:) return (:create a new movie node :) {for $y in $x/(node()[not(self::director)] union @*) (: iterate over all children and attributes of $x, and keep those nodes that are not director nodes by applying predicate (node()[not(self::director)])and all attributes :) return $y }

26 V. Christophides, I. Fundulaki, CS561 Spring 2010 Transform Expressions: Exercise

 Return all movies, omitting their budget and replacing the value of budget with “unknown”

 Write this as an XQuery expression

27 V. Christophides, I. Fundulaki, CS561 Spring 2010 Implementations

 XQuery Update: eXist (Open Source for XML Documents) http://exist.sourceforge.net/

MonetDB (Open Source Database System) http://monetdb.cwi.nl/projects/monetdb/XQuery/index.

Galax (Version 1.1) http://www.galaxquery.org/

28