Xquery Update
Total Page:16
File Type:pdf, Size:1020Kb
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 <movie title=“Before Night Falls”/> as first into $doc/movies <movies> <movies> <movie title="The Big Lebowski"> <director>E. Cohen</director> <movie title=“Before Night Falls”/> <director>J. Cohen</director> </movie> <movie title="The Big Lebowski"> <movie title="All President's Men"> <director>E. Cohen</director> <director>A. J. Pakula</director> <director>J. Cohen</director> </movie> </movie> </movies> <movie title="All President's Men"> <director>A. J. Pakula</director> </movie> </movies> 10 V. Christophides, I. Fundulaki, CS561 Spring 2010 Insert Expressions: Examples insert node <movie title=“Before Night Falls”/> before $doc//movie[director=“A.J. Pakula”] <movies> <movie title="The Big Lebowski"> <movies> <director>E. Cohen</director> <movie title="The Big Lebowski"> <director>J. Cohen</director> <director>E. Cohen</director> </movie> <director>J. Cohen</director> <movie title="All President's Men"> </movie> <director>A. J. Pakula</director> </movie> <movie title=“Before Night Falls”/> <movie title=“The Parallax View”> <producer>A. J. Pakula</producer> <movie title="All President's Men"> </movie> <director>A. J. Pakula</director> </movies> </movie> <movie title=“The Parallax View”> <producer>A. J. Pakula</producer> </movie> </movies> $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 <movies> <movie title="All President's Men"> <movies> <director>A. J. Pakula</director> <movie title="All President's Men"> </movie> <director>A. J. Pakula</director> <movie title=“A night on Earth”> </movie> <director>J. Jarmush</director> </movie> <movie title="The Big Lebowski"> </movies> <director>E. Cohen</director> <director>J. Cohen</director> </movie> doc1 <movie title=“A night on Earth”> <director>J. Jarmush</director> <comedies> <movie title="The Big Lebowski"> </movie> <director>E. Cohen</director> </movies> <director>J. Cohen</director> </movie> <comedies> 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”] <movies> <movies> <movie title="The Big Lebowski"> <director>E. Cohen</director> <movie title="The Big Lebowski"> <director>J. Cohen</director> <director>E. Cohen</director> </movie> <director>J. Cohen</director> <movie title="All President's Men"> </movie> <director>A. J. Pakula</director> </movie> <movie title="All President's Men"> </movies> <director>A. J. Pakula</director> </movie> </movies> <movies> <movie title="All President's Men"> <director>A. J. Pakula</director> </movie> </movies> 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] <movies> <documentaries> <movie title="The Big Lebowski"> <documentary <director>E. Cohen</director> title=“The Corporation”/> <director>J. Cohen</director> <documentary </movie> title=“Enron, the smartest guys <movie title="All President's Men"> in the room”/> <director>A. J. Pakula</director> </documentaries> </movie> </movies> <movies> <documentary title=“Enron, the smartest guys in the room”/> <movie title="All President's Men"> <director>A. J. Pakula</director> </movie> </movies> 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] <movies> <movies> <movie title="The Big Lebowski"> <movie title="The Big Lebowski"> <director>E. Cohen</director> <director>E. Cohen</director> <director>J. Cohen</director> </movie> <director>Alan J. Pakula</director> <movie title="All President's Men"> <director>A. J. Pakula</director> </movie> </movie> <movie title="All President's Men"> </movies> <director>A. J. Pakula</director> </movie> </movies> 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 <movies> <movies> <movie title="The Big Lebowski"> <movie title="The Big Lebowski"> <director>E. Cohen</director> <director>E. Cohen</director> <director>J. Cohen</director> <director>J. Cohen</director> </movie> </movie> <movie title="All President's Men"> <movie title="All President's Men"> <director>A. J. Pakula</director> </movie> <main-director>A. J. Pakula </movies> </main-director> </movie> </movies> 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