Updating XML with XQuery

Updating XML with XQuery Web Data Management and Distribution Ioana Manolescu-Goujot INRIA SaclayÎle-de-France and LRI, Université Paris XI http://w...
Author: Melinda Conley
7 downloads 0 Views 181KB Size
Updating XML with XQuery

Web Data Management and Distribution Ioana Manolescu-Goujot INRIA SaclayÎle-de-France and LRI, Université Paris XI http://www-rocq.inria.fr/manolesc

January 4, 2010

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

1 / 21

Basics

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. SQL parallel: without

select... from... where...

insert into table... update table... Applications require reading and updating XML data.

XQuery Update Facility: a working draft, not yet a specication http://www.w3.org/TR/xquery-update-10/

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

2 / 21

Principles

Requirements for the XQuery Update language

Expressive power: Insert Delete

Update Copy with new identity

Extension of XQuery itself: Simplies understanding and learning the language Diculty to introduce side eects... Well-dened semantics Conciseness Amenable to ecient implementation...

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

3 / 21

Principles

XQuery Update concepts

All XQuery expressions can be classied into: Updating expressions Non-updating expressions XQuery Update introduces ve new kind of expressions: insert, delete, replace, rename: updating expressions transform: non-updating expression XQuery Update species: how all XQuery expressions are classied into updating and non-updating places where each type of expression can appear syntax and semantics of each new expression

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

4 / 21

Principles

XQuery Update processing model

The evaluation of an expression produces: an instance of the XQuery Data Model and a pending update list: set of update primitives, i.e. node stat changes that have to be applied. In the current specication, one of the two has to be empty. This may change in the future. (The evaluation of a simple XQuery produces an instance of the XQuery Data Model.) Each update primitive has a target node. Update primitives are checked for conicts, and if no conict appears, they are applied.

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

5 / 21

XQuery updates

Insert expressions

Insert is an updating expression. General form: insert (constructor|({expr})) (as (rst|last))? into (after|before) expr The rst expression is called the source, and the second the target. The source and target expressions must not be updating. insert < year >2005 after doc ( " bib . xml " )/ books / book [1]/ published insert $article / author as last into doc ( " bib . xml " )/ books / book [3]

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

6 / 21

XQuery updates

Insert expressions

The pending update list is obtained as follows: evaluate the update target (which are the nodes that should get new children) for each such node, add to the pul the corresponding add-child operation insert { $new - police - report } as last into doc ( " insurance . xml " )// policies / policy [ id = $pid ] / driver [ licence = $licence ]/ accident [ date = $dateacc ] / police - reports

locate the proper police-reports element for each element in $new-police-report, add an add-last-child operation to the pul I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

7 / 21

XQuery updates

Delete expressions

Delete is an updating expressions. Its produces a non-empty pending update list. General form: delete expr delete doc ( " bib . xml " )/ books / book [1]/ author [ last ()] delete / email / message [ fn : currentDate () - date > xdt : dayTimeDuration ( P365D )]

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

8 / 21

XQuery updates

Replace expressions

Replace is an updating expression. It produces a non-empty pending update list. General form: replace expr with expression replace doc ( " bib . xml " )/ books / book [1]/ publisher with doc ( " bib . xml " )/ books / book [2]/ publisher replace value of doc ( " bib . xml " )/ books / book [1]/ price with doc ( " bib . xml " )/ books / book [1]/ price *1.1

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

9 / 21

XQuery updates

Rename expression

Rename is an updating expression. General form: rename expr to expr rename doc ( " bib . xml " )/ books / book [1]/ author [1] to main - author rename doc ( " bib . xml " )/ books / book [1]/ author [1] to $newname

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

10 / 21

XQuery updates

Transform expressions (1)

Transform is a non-updating expression. General form: copy $varName := expr (, $varName := expr )* modify expr return expr Example: return all managers, omiting their salaries and replacing them with an attribute xsi:nil.

Remark

It can be done with XQuery. But it's painful! Transform returns a modied copy, without impacting the original database (it is a non-updating expression).

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

11 / 21

XQuery updates

Transform expressions (2)

Document

Smith 100000 Jones 60000 Roberts 150000

Desired result

Smith Roberts

It can be done with XQuery. But it is dicult! Exercise... I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

12 / 21

XQuery updates

Transform expressions (3)

Return all managers, omiting their salaries and replacing them with an attribute xsi:nil. for $e in doc ( " employees . xml " )// employee where $e / @manager = true () return copy $emp := $e modify ( replace value of node $emp / salary with " " , insert nodes ( attribute xsi : nil { " true " }) into $emp / salary ) return $em

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

13 / 21

More complex example

Programming with XQuery Update

Address book synchronization: One archive version and two copies c1 = a and c2 6= a ⇒ propagate c2 to a and c1 c1 6= a, c2 6= a ⇒ I I

If possible, merge dierences and propagate them to a, then to c1 , c2 Otherwise, raise an error.

Agenda entries are of the form: < entry > < name > Benjamin < contact > benjamin@inria . fr < entry > < name > Anthony < contact > tony@uni - toulon . fr I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

14 / 21

More complex example

Programming with XQuery Update

for $a in doc ( " archive . xml " )// entry , $v1 in doc ( " copy1 . xml " )/ version / entry , $v2 in doc ( " copy2 . xml " )/ version / entry where $a / name = $v1 / name and $v1 / name = $v2 / name return if ( $a / contact = $v1 / contact and $v1 / contact = $v2 / contact ) then () else if ( $v1 / contact = $v2 / contact ) then replace value of node $a / contact with $v1 / contact else if ( $a / contact = $v1 / contact ) then ( replace value of $a / contact with $v2 / contact , replace value of $v1 / contact with $v2 / contact ... I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

15 / 21

More complex example

Programming with XQueryUpdate ...

if ( $a / contact = $v1 / contact ) then ... else if ( $a / contact = $v2 / contact ) then ( replace value of $a / contact with $v1 / contact , replace value of $v2 / contact with $v1 / contact ) else ( insert node < fail > < arch >{ $a } { $v1 } { $v2 } into doc ( " log . xml " )/ log ) , replace value of node doc ( " archive . xml " ) /*/ last - synch - time with current - dateTime () I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

16 / 21

Perspective on XQuery dialects

XQuery - SQL comparison

Function

Relational

XML

XQuery Query (read-only) SQL select XQuery Update Update SQL update XQuery Full-Text Full-text SQL MMS XQuery Scripting Extension Scripting PL/SQL XQuery update is not a programming language. Missing: Control over the scope of snapshots, i.e. when do my updates become visible to another query? XQuery Update: after the current query has nished executing. Control over atomicity, i.e. which expressions must be executed atomically? The possibility to both return a result and have side eects. XQuery Update: one or the other is empty. Error handling. I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

17 / 21

Perspective on XQuery dialects

An XQuery scripting language: XQuery-P

D.Chamberlin, M.Carey, D.Florescu, D.Kossman: "Programming with XQuery", XIME-P 2006 1

2

3

Dene a sequential execution mode: the statements must be evaluated in order, and each statement sees the side eect of the previous one Dene blocks, which are units of code to be executed sequentially. New variables can be dened inside a block. The returned result is that of the last expression. Introduce assignments to bind variables to new values.

for $item in / catalog / item [ price < 100] return { replace value of $item / price with $item / price * 1.1; $item }

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

18 / 21

Perspective on XQuery dialects

An XQuery scripting language: XQuery-P

Forces to dene evaluation order on an XQuery expression: for, let, where, order by executed in the order of their appearance; then, return if evaluated rst, then evaluate then or else ,: evaluate from left to right, apply all the updates after each item function call: evaluate the arguments before the body Specifying evaluation order is a big departure from traditional query language style. (Which of select, from and where is evaluated rst?)

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

19 / 21

Perspective on XQuery dialects

Programming with XQuery-P declare updating function local : transfer ( $from - acctno as xs : string , $to - acctno as xs : string , $amount as xs : decimal ) as xs : integer { declare $from - acct as element ( account ) := / bank / account [ acctno eq $from - acctno ] , $to - acct as element ( account ) := / bank / account [ acctno eq $to - acctno ]; if ( $from - acct / balance > $amount ) then atomic { do replace value of $from - acct / balance with $from - acct / balance - $amount ; do replace value of $to - acct / balance with $to - acct / balance + $amount ; 0 } (: end of atomic region :) else -1 }; I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

20 / 21

Perspective on XQuery dialects

Implementations

XQuery Update: eXist MonetDB XQuery-P and similar proposals: preliminary prototypes

I. Manolescu (Leo team)

Updating XML with XQuery

January 4, 2010

21 / 21

Suggest Documents