sdpl 20077: querying xml with xquery1 7 querying xml n how to access xml data sources? n xquery, xml...

52
SDPL 2007 7: Querying XML with XQuer y 1 7 Querying XML 7 Querying XML How to access XML data sources? How to access XML data sources? XQuery XQuery , XML Query Lang, W3C Rec, 01/ '07 , XML Query Lang, W3C Rec, 01/ '07 joint work by XML Query and XSL WGs joint work by XML Query and XSL WGs » with XPath 2.0 and XSLT 2.0 with XPath 2.0 and XSLT 2.0 influenced by many research groups and query influenced by many research groups and query languages languages » Quilt Quilt , , XPath XPath , XQL, XML-QL, SQL, OQL, , XQL, XML-QL, SQL, OQL, Lorel, ... Lorel, ... A query language for any XML-represented A query language for any XML-represented data: data: both documents and databases both documents and databases

Upload: dayna-barker

Post on 14-Jan-2016

234 views

Category:

Documents


0 download

TRANSCRIPT

SDPL 2007 7: Querying XML with XQuery 1

7 Querying XML7 Querying XML

How to access XML data sources?How to access XML data sources? XQueryXQuery, XML Query Lang, W3C Rec, 01/ '07, XML Query Lang, W3C Rec, 01/ '07

– joint work by XML Query and XSL WGsjoint work by XML Query and XSL WGs» with XPath 2.0 and XSLT 2.0with XPath 2.0 and XSLT 2.0

– influenced by many research groups and query languagesinfluenced by many research groups and query languages» QuiltQuilt, , XPathXPath, XQL, XML-QL, SQL, OQL, Lorel, ..., XQL, XML-QL, SQL, OQL, Lorel, ...

– A query language for any XML-represented data: A query language for any XML-represented data: both documents and databasesboth documents and databases

SDPL 2007 7: Querying XML with XQuery 2

Functional Requirements Functional Requirements (1)(1)

Support operations (selection, projection, aggregation, Support operations (selection, projection, aggregation, sorting, etc.) on all data types: sorting, etc.) on all data types: – Choose data based on content or structureChoose data based on content or structure– Operate on document hierarchy and orderOperate on document hierarchy and order

Structural preservation and transformation:Structural preservation and transformation: – Preserve relative hierarchy and sequence of input structuresPreserve relative hierarchy and sequence of input structures– Transform XML structures, and create newTransform XML structures, and create new

Combining and joining:Combining and joining:– Combine data from different parts of a document, or from multiple Combine data from different parts of a document, or from multiple

documentsdocuments

SDPL 2007 7: Querying XML with XQuery 3

Functional Requirements Functional Requirements (2)(2)

Closure property:Closure property: – Results of XML queries are also XML Results of XML queries are also XML

(well-formed document (well-formed document fragmentsfragments))– > queries can be combined without limit> queries can be combined without limit

Extensibility:Extensibility:– should support externally defined functions on all data should support externally defined functions on all data

types of the data modeltypes of the data model

SDPL 2007 7: Querying XML with XQuery 4

XQuery in a NutshellXQuery in a Nutshell

Functional expression languageFunctional expression language Strongly-typedStrongly-typed: : (optional) (optional) type-checkingtype-checking of expressions, and of expressions, and

validationvalidation of results (We’ll concentrate to of results (We’ll concentrate to processingprocessing))– predeclared prefixpredeclared prefix for type names: for type names: xsxs==http://www.w3.org/2001/http://www.w3.org/2001/XMLSchemaXMLSchema

Extends XPath 2.0Extends XPath 2.0– XQuery 1.0 and XPath 2.0XQuery 1.0 and XPath 2.0 Functions and OperatorsFunctions and Operators, Rec. Jan. , Rec. Jan.

20072007 XQuery XQuery XPath' + XSLT' + SQL' (roughly) XPath' + XSLT' + SQL' (roughly)

SDPL 2007 7: Querying XML with XQuery 5

Example QueryExample Query

xquery version "1.0";xquery version "1.0";

<cheapBooks><cheapBooks> <Title>Cheap Books</Title> <Title>Cheap Books</Title> { { forfor $b $b inin fn:doc("bib.xml")//book[@price < 50] fn:doc("bib.xml")//book[@price < 50] order byorder by $b/title $b/title returnreturn $b } $b } </cheapBooks> </cheapBooks>

XML-based syntax (XQueryX) also specifiedXML-based syntax (XQueryX) also specified

Syntax "concise and easily understood"Syntax "concise and easily understood"

SDPL 2007 7: Querying XML with XQuery 6

A possible resultA possible result

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><cheapBooks><cheapBooks> <Title>Cheap Books</Title> <Title>Cheap Books</Title> <book price="26.50"> <book price="26.50"> <title>Computing with Logic</title> <title>Computing with Logic</title> <author>David Maier</author> <author>David Maier</author> <publisher>Benjamin Cummings</publisher> <publisher>Benjamin Cummings</publisher> <year>1999</year> <year>1999</year> </book> </book> <book price="40.00"> <book price="40.00"> <title>Designing Internet applications</title> <title>Designing Internet applications</title> <author>Michael Leventhal</author> <author>Michael Leventhal</author> <publisher>Prentice Hall</publisher> <publisher>Prentice Hall</publisher> <year>1998</year> <year>1998</year> </book> </book></cheapBooks></cheapBooks>

SDPL 2007 7: Querying XML with XQuery 7

XQuery: BasicsXQuery: Basics

A query is an A query is an expression expression (lauseke)(lauseke) Expressions operate on, and return Expressions operate on, and return sequencessequences of of

– atomic valuesatomic values (of XML Schema simple types) and (of XML Schema simple types) and– nodesnodes (of 6 types = XPath node types (of 6 types = XPath node types –– NS nodes) NS nodes)– an an itemitem a a singleton sequencesingleton sequence– sequences are sequences are flatflat: no sequences as items: no sequences as items

» (1(1,, (2 (2,, 3) 3),, () (),, 1) = (1 1) = (1,, 2 2,, 3 3,, 1) 1)

– sequences are sequences are orderedordered, and can contain duplicates , and can contain duplicates Unlimited combination of expressions, often with automatic Unlimited combination of expressions, often with automatic

type conversions (e.g. for arithmetics)type conversions (e.g. for arithmetics)

SDPL 2007 7: Querying XML with XQuery 8

XQuery: Accessing DocumentsXQuery: Accessing Documents

XQuery operates on nodes accessible by input XQuery operates on nodes accessible by input functionsfunctions– fn:doc(fn:doc(""URIURI""))

» document-nodedocument-node of the XML document available at of the XML document available at URIURI

» same as same as document(document(""URIURI"")) in XSLT 1.0 in XSLT 1.0

– fn:collection(fn:collection(""URIURI""))» sequence of nodes from sequence of nodes from URIURI

– predeclared prefix for the default function namespace: predeclared prefix for the default function namespace: fnfn=http://www.w3.org/2005/04/xpath-functions=http://www.w3.org/2005/04/xpath-functions

SDPL 2007 7: Querying XML with XQuery 9

Central XQuery ExpressionsCentral XQuery Expressions

Path expressionsPath expressions Sequence expressions Sequence expressions Comparison operatorsComparison operators Quantified expressions Quantified expressions

((somesome//everyevery $x… $x… in … satisfiesin … satisfies …) …) Element constructors (Element constructors ( XSLT templates) XSLT templates) FLWOR expressions FLWOR expressions

(”flower”; (”flower”; fforor--lletet--wwherehere--oorder byrder by--rreturneturn)) and others, in examples ... and others, in examples ...

also in XPath 2.0also in XPath 2.0

SDPL 2007 7: Querying XML with XQuery 10

Path ExpressionsPath Expressions

Similar to XPath 1.0: [/]Expr/… /ExprSimilar to XPath 1.0: [/]Expr/… /Expr– but steps more liberal:but steps more liberal:– arbitrary (node-sequence valued) expressions OKarbitrary (node-sequence valued) expressions OK– 6 (of 13 XPath) axes: 6 (of 13 XPath) axes: childchild,, descendant descendant,, attribute attribute,, self self,,

descendant-or-selfdescendant-or-self,, parent parent» others (except others (except namespacenamespace) available if ) available if Full Axis FeatureFull Axis Feature

supportedsupported» with document-order operators (with document-order operators (<<<<, , >>>>) sufficient for ) sufficient for

expressing queries expressing queries ((→→ Exercises) Exercises)

– produce a sequence of nodes in document order, without produce a sequence of nodes in document order, without duplicatesduplicates

SDPL 2007 7: Querying XML with XQuery 11

Sequence ExpressionsSequence Expressions

Constant sequences constructed by listing Constant sequences constructed by listing valuesvalues– comma (comma (,,) is a catenation operator) is a catenation operator

» (1(1,, (2 (2,, 3) 3),, () (),, 1) = (1 1) = (1,, 2 2,, 3 3,, 1) 1)

Also dynamically generated:Also dynamically generated:– (<start />, $emp/child, <end/>)(<start />, $emp/child, <end/>)

Shorthands for numeric sequences:Shorthands for numeric sequences:– 1 1 toto 4 4– 4 4 toto 1 1– fn:reverse(1 fn:reverse(1 toto 4) 4)

(1, 2, 3, 4)(1, 2, 3, 4)

()() (4, 3, 2, 1)(4, 3, 2, 1)

SDPL 2007 7: Querying XML with XQuery 12

Set Operations on Node (!) SequencesSet Operations on Node (!) Sequences

Assume variable bindings:Assume variable bindings:

$s2$s2

Then:Then:

$s1$s1

$s1 $s1 unionunion $s2 = $s2 =

$s1 $s1 intersectintersect $s2 = $s2 =

$s1 $s1 exceptexcept $s2 = $s2 =

based on based on node indentitynode indentity((nodenode11 is is nodenode22))

aa bb cc dd ee

aa bb cc dd ee

cc

aa bb

SDPL 2007 7: Querying XML with XQuery 13

Node ComparisonsNode Comparisons

To compare single nodes, To compare single nodes, – for identity: for identity: isis

$book//chap[@id="ch1"] $book//chap[@id="ch1"] isis ($book//chap)[1] ($book//chap)[1] true iff the chapter with true iff the chapter with id="ch1"id="ch1" is indeed the first is indeed the first

– for document order: for document order: <<<< andand >>>>$book//chap[@id="ch2"] $book//chap[@id="ch2"] >>>>

$book//title[. eq "Intro"] $book//title[. eq "Intro"] true iff the chapter with true iff the chapter with id="ch2"id="ch2" appears appears afterafter <title>Intro</title><title>Intro</title>

SDPL 2007 7: Querying XML with XQuery 14

Comparing values of sequences and itemsComparing values of sequences and items

General comparisonsGeneral comparisons btw sequences: btw sequences: – ==, , !=!=, , <<, , <=<=, , >>, , >=>=– existential semantics: true iff existential semantics: true iff somesome pair of values pair of values

from operand sequences satisfy the conditionfrom operand sequences satisfy the condition » (1,2) = (2,3); (2,3) = (3,4); (1,2) != (3,4)(1,2) = (2,3); (2,3) = (3,4); (1,2) != (3,4)» Same as in XPath 1.0:Same as in XPath 1.0:

//book[author = "Aho"]//book[author = "Aho"]→→ books where books where somesome author is Aho author is Aho

Value comparisonsValue comparisons btw single values: btw single values:– eqeq, , nene, , ltlt, , lele, , gtgt, , gege

» 1 eq 3 - 2; 10 lt 20; $books[@price le 100]1 eq 3 - 2; 10 lt 20; $books[@price le 100]

SDPL 2007 7: Querying XML with XQuery 15

Filter ExpressionsFilter Expressions

Location steps can be filtered by predicates:Location steps can be filtered by predicates: $book//(chap | app)[fn:last()]/title$book//(chap | app)[fn:last()]/title

the title of the last chapter of appendix, whichever is lastthe title of the last chapter of appendix, whichever is last

Other sequences, too:Other sequences, too: (1 to 20)[. mod 5 eq 0](1 to 20)[. mod 5 eq 0] →→ (5, 10, 15, 20) (5, 10, 15, 20) – ('('..' generalized from XPath 1.0 shorthand for ' generalized from XPath 1.0 shorthand for self::node()self::node() into the into the context itemcontext item))

XPath 2.0 extended stepXPath 2.0 extended step

SDPL 2007 7: Querying XML with XQuery 16

Element ConstructorsElement Constructors

Similar to XSLT templates:Similar to XSLT templates:– start and end tag enclosing the contentstart and end tag enclosing the content– literal fragments written directly, literal fragments written directly,

expressions enclosed in braces expressions enclosed in braces {{ and and }}≈≈ XSLT 1.0 attribute value templates XSLT 1.0 attribute value templates

often used inside another expression that binds often used inside another expression that binds variables used in the element constructorvariables used in the element constructor– (There is no 'current node' in XQuery)(There is no 'current node' in XQuery)– See nextSee next

SDPL 2007 7: Querying XML with XQuery 17

ExampleExample

An An empemp element with an element with an empidempid attribute and child attribute and child elements elements namename and and jobjob, from values in variables , from values in variables $id$id, , $n$n, and , and $j$j::

<emp empid="<emp empid="{$id}{$id}">">

<name><name>{$n}{$n}</name> </name>

<job><job>{$j}{$j}</job></job>

</emp></emp>

Also Also computed constructorscomputed constructors::

element {"emp"} { element {"emp"} { attribute {"empid"} attribute {"empid"}{$id}{$id},, <name> <name> {$n}{$n} </name>, </name>, <job> <job> {$j}{$j} </job> } </job> }

SDPL 2007 7: Querying XML with XQuery 18

FLWOR ("flower") ExpressionsFLWOR ("flower") Expressions

Constructed from Constructed from forfor, , letlet, , wherewhere, , order byorder by and and returnreturn clauses (~SQL clauses (~SQL selectselect--fromfrom--wherewhere))

Syntax: Syntax: (ForClause | LetClause)+ (ForClause | LetClause)+ WhereClause? WhereClause? OrderByClause?OrderByClause?""returnreturn" Expr" Expr

FLWOR binds variables to values, and uses FLWOR binds variables to values, and uses these bindings to construct a result these bindings to construct a result (an ordered sequence of items)(an ordered sequence of items)

SDPL 2007 7: Querying XML with XQuery 19

Flow of data in a FLWOR expressionFlow of data in a FLWOR expression

SDPL 2007 7: Querying XML with XQuery 20

forfor clauses clauses

forfor $V $V11 inin ExpExp11 ( (, ,

$V$V22 inin ExpExp2,2, …) …)

– associates each variable associates each variable VVii with expression with expression ExpExpii

(e.g. a path expression) (e.g. a path expression)

Result: list of tuples, each containing a binding for Result: list of tuples, each containing a binding for each of the variableseach of the variables

can be though of as loops iterating over the items can be though of as loops iterating over the items returned by respective expressionsreturned by respective expressions

SDPL 2007 7: Querying XML with XQuery 21

Example:Example: for for clause clause

forfor $i $i inin (1,2), (1,2), $j $j inin (1 (1 toto $i) $i)returnreturn <tuple> <tuple>

<i>{<i>{$i$i}</i> <j>{}</i> <j>{$j$j}</j></tuple>}</j></tuple>

Result:Result:

<tuple><i>1</i><j>1</j></tuple><tuple><i>1</i><j>1</j></tuple>

<tuple><i>2</i><j>1</j></tuple><tuple><i>2</i><j>1</j></tuple>

<tuple><i>2</i><j>2</j></tuple><tuple><i>2</i><j>2</j></tuple>

SDPL 2007 7: Querying XML with XQuery 22

letlet clauses clauses

letlet also binds variables to expressions also binds variables to expressions– each variable gets the entire sequence as its value each variable gets the entire sequence as its value

(without iterating over the items of the sequence)(without iterating over the items of the sequence)– results in binding a single sequence for each variableresults in binding a single sequence for each variable

Compare:Compare:– forfor $b $b inin doc("bib.xml")//book doc("bib.xml")//book

many bindings (to single books) many bindings (to single books)– letlet $bl $bl :=:= doc("bib.xml")//book doc("bib.xml")//book

a single binding (to sequence of books) a single binding (to sequence of books)

SDPL 2007 7: Querying XML with XQuery 23

Example: let clausesExample: let clauses

letlet $s $s :=:= (<one/>, <two/>, <three/>) (<one/>, <two/>, <three/>)returnreturn <out> {$s} </out> <out> {$s} </out>

Result:Result:

<out><out> <one/><one/> <two/><two/> <three/><three/></out></out>

forfor $s $s inin (<one/>,<two/>,<three/>) (<one/>,<two/>,<three/>)returnreturn <out> {$s} </out> <out> {$s} </out>

-->--> <out><one/></out><out><one/></out> <out><two/></out><out><two/></out> <out><three/></out><out><three/></out>

SDPL 2007 7: Querying XML with XQuery 24

forfor//letlet clauses clauses

A FLWOR expr may contain several A FLWOR expr may contain several forfors and s and letletss– each may refer to variables bound in previous clauseseach may refer to variables bound in previous clauses

the result of the the result of the forfor//letlet sequence: sequence:– an ordered list of tuples (an ordered list of tuples (monikkomonikko) of bound variables) of bound variables– number of tuples = product of the cardinalities of the number of tuples = product of the cardinalities of the

sequences returned by the sequences returned by the forfor expressions expressions

SDPL 2007 7: Querying XML with XQuery 25

wherewhere clause clause

binding tuples generated by binding tuples generated by forfor and and letlet clauses clauses are filtered by an optional are filtered by an optional wherewhere clause clause– tuples with a tuples with a truetrue condition are used to instantiate the condition are used to instantiate the

returnreturn clause clause

the the wherewhere clause may contain several predicates clause may contain several predicates connected by connected by andand, , oror, and , and fn:notfn:not()()– usually refer to the bound variablesusually refer to the bound variables

– sequences as Booleans (similarly to node-sets in sequences as Booleans (similarly to node-sets in XPath 1.0): empty ~ XPath 1.0): empty ~ falsefalse; non-empty ~ ; non-empty ~ truetrue

SDPL 2007 7: Querying XML with XQuery 26

wherewhere clause clause

forfor binds variables to single items binds variables to single items value comparisonsvalue comparisons, e.g. , e.g. $color $color eqeq ""redred""

letlet to whole sequences to whole sequences general comparisonsgeneral comparisons, e.g. , e.g. $colors $colors == "red" "red"

((~ ~ somesome $c $c inin $colors $colors satisfiessatisfies $c $c eqeq "red" "red"))

– a number of aggregation functions available: a number of aggregation functions available: avgavg()(), , sumsum()(), , countcount()(), , maxmax()(), , minmin()()

(also in XPath 1.0)(also in XPath 1.0)

SDPL 2007 7: Querying XML with XQuery 27

returnreturn clause clause

The The returnreturn clause generates the output of the clause generates the output of the FLWOR expressionFLWOR expression

instantiated once for each binding tupleinstantiated once for each binding tuple often contains element constuctors, references to often contains element constuctors, references to

bound variables, and nested sub-expressionsbound variables, and nested sub-expressions

SDPL 2007 7: Querying XML with XQuery 28

Example:Example: for for + + returnreturn

forfor $i $i inin (1,2), (1,2), $j $j inin (1 (1 toto $i) $i)returnreturn <tuple> <tuple>

<i>{<i>{$i$i}</i> <j>{}</i> <j>{$j$j}</j></tuple>}</j></tuple>

Result:Result:

<tuple><i>1</i><j>1</j></tuple><tuple><i>1</i><j>1</j></tuple>

<tuple><i>2</i><j>1</j></tuple><tuple><i>2</i><j>1</j></tuple>

<tuple><i>2</i><j>2</j></tuple><tuple><i>2</i><j>2</j></tuple>

SDPL 2007 7: Querying XML with XQuery 29

Examples (modified from "XML Query Use Cases")Examples (modified from "XML Query Use Cases")

Assume: a document named ”Assume: a document named ”bib.xmlbib.xml” ” containing of a list of containing of a list of bookbooks:s:

<book><book>++

<title><title><author><author>++

<publisher><publisher><year><year><price><price>

SDPL 2007 7: Querying XML with XQuery 30

<recent-MK-books> {<recent-MK-books> {

} </recent-MK-books>} </recent-MK-books>

List Morgan Kaufmann book titles since 1998List Morgan Kaufmann book titles since 1998

forfor $b $b in docin doc(”bib.xml”)//book(”bib.xml”)//book

wherewhere $b/publisher = ”Morgan Kaufmann” $b/publisher = ”Morgan Kaufmann”

andand $b/year >= 1998 $b/year >= 1998

returnreturn <book year="{$b/year}"> <book year="{$b/year}">

{$b/title}{$b/title}

</book></book>

SDPL 2007 7: Querying XML with XQuery 31

Result could be...Result could be...

<recent-MK-books><recent-MK-books> <book year=”1999”><book year=”1999”> <title>TCP/IP Illustrated</title><title>TCP/IP Illustrated</title> </book></book> <book year=”2000”><book year=”2000”> <title>Advanced Programming in the Unix <title>Advanced Programming in the Unix environment</title>environment</title> </book></book></recent-MK-books></recent-MK-books>

SDPL 2007 7: Querying XML with XQuery 32

Publishers with avg price of their books:Publishers with avg price of their books:

forfor $p $p in fn:distinct-valuesin fn:distinct-values(( fn:doc(”bib.xml”)//publisher ) fn:doc(”bib.xml”)//publisher )

letlet $a := avg( doc(”bib.xml”)//book[ $a := avg( doc(”bib.xml”)//book[publisher = $p]/price )publisher = $p]/price )

returnreturn <publisher> <publisher><name> {$p} </name><name> {$p} </name><avgprice> {$a} </avgprice><avgprice> {$a} </avgprice>

</publisher> </publisher>

string values of the string values of the sequence, without sequence, without duplicatesduplicates

SDPL 2007 7: Querying XML with XQuery 33

Invert the book-list structureInvert the book-list structure

<author_list>{ (: group books by authors :)<author_list>{ (: group books by authors :)

forfor $a $a in in distinct-values(distinct-values(doc(”bib.xml”)//author )doc(”bib.xml”)//author )

returnreturn <author> <author> {{

<name> <name> {{$a$a}} </name> </name>,,

forfor $b $b in in doc(”bib.xml”)//book[doc(”bib.xml”)//book[

author = $a]author = $a]

returnreturn $b/title $b/title }}

</author></author>

} </author_list>} </author_list>

SDPL 2007 7: Querying XML with XQuery 34

List of publishers alphabetically, List of publishers alphabetically, and their books in descending order of priceand their books in descending order of price

forfor $p $p in in distinct-values(distinct-values(doc(”bib.xml”)//publisher )doc(”bib.xml”)//publisher )

order byorder by $p $preturnreturn <publisher><publisher> <name>{$p}</name><name>{$p}</name> { { forfor $b $b in in doc(”bib.xml”)//book[doc(”bib.xml”)//book[

publisher = $p]publisher = $p] order byorder by $b/price $b/price descendingdescending returnreturn <book> {$b/title, <book> {$b/title, $b/price} </book> }$b/price} </book> } </publisher></publisher>

SDPL 2007 7: Querying XML with XQuery 35

Queries on Document OrderQueries on Document Order

Operators Operators <<<< and and >>>>: :

– x x <<<< y = true iff node x precedes node y in document y = true iff node x precedes node y in document order; (y order; (y >>>> x similarly) x similarly)

Consider a surgical report with Consider a surgical report with

– procedure procedure elements containing elements containing

» incision sub-incision sub-elements elements

Return a "critical sequence" of contents between the first Return a "critical sequence" of contents between the first and the second incisions of the first procedureand the second incisions of the first procedure

SDPL 2007 7: Querying XML with XQuery 36

Computing a "critical sequence"Computing a "critical sequence"

<critical_sequence> {<critical_sequence> { letlet $p := $p := (doc("report.xml")//procedure)[1](doc("report.xml")//procedure)[1] forfor $n $n inin $p/node() $p/node() wherewhere $n $n >>>> ($p//incision)[1] ($p//incision)[1] andand

$n $n <<<< ($p//incision)[2] ($p//incision)[2] returnreturn $n } $n } </critical_sequence></critical_sequence>

NB: if NB: if incisionincisions are not children of the s are not children of the procedureprocedure, , then an ancestor of the second then an ancestor of the second incisionincision gets to the gets to the result; How to avoid this?result; How to avoid this?

SDPL 2007 7: Querying XML with XQuery 37

User-defined functions: ExampleUser-defined functions: Example

declare functiondeclare function local:precedes($a local:precedes($a asas node(), node(), $b $b asas node()) node()) asas xs:boolean xs:boolean

{ $a { $a <<<< $b $b and and (: $a is no ancestor of $b: :) (: $a is no ancestor of $b: :) empty(empty($a//node() intersect $b$a//node() intersect $b)) }; };

locallocal:: is predeclared prefix for the namespace of local is predeclared prefix for the namespace of local function namesfunction names

– Alternatively: Alternatively:

declare namespace declare namespace my=http://my.namespace.org;my=http://my.namespace.org; declare functiondeclare function my:precedes( my:precedes(... (as above)... (as above)

SDPL 2007 7: Querying XML with XQuery 38

User-defined functions: ExampleUser-defined functions: Example

Now, ”critical sequence” without ancestors of Now, ”critical sequence” without ancestors of incisionincision: :

<critical_sequence> {<critical_sequence> { letlet $p := $p :=

(doc("report.xml")//procedure)[1](doc("report.xml")//procedure)[1] forfor $n $n inin $p/node() $p/node() wherewhere $n $n >>>> ($p//incision)[1] ($p//incision)[1] andand

local:precedes(local:precedes($n, $n, ($p//incision)[2]($p//incision)[2]))

returnreturn $n $n} </critical_sequence>} </critical_sequence>

SDPL 2007 7: Querying XML with XQuery 39

Recursive TransformationsRecursive Transformations

Example: “Table-of-contents” for nested sectionsExample: “Table-of-contents” for nested sections– NB NB if-then-else if-then-else (in ordinary XPath 2.0 expressions, too)(in ordinary XPath 2.0 expressions, too)

declare namespace declare namespace my=http://my.own-ns.org;my=http://my.own-ns.org;

declare functiondeclare function my:toc( $n my:toc( $n asas element() ) element() ) asas element()* element()*

{ { if if ((name(name($n$n))=”sect”)=”sect”) thenthen <sect> { <sect> {

forfor $c $c inin $n/* $n/* returnreturn my:toc($c) } </sect> my:toc($c) } </sect> else if else if ((name(name($n$n))=”title”) =”title”) thenthen $n $n else else (: check child elements, if any: :)(: check child elements, if any: :)

forfor $c $c inin $n/* $n/* returnreturn my:toc($c) }; my:toc($c) };

SDPL 2007 7: Querying XML with XQuery 40

Querying relational dataQuerying relational data

Lots of data is stored in relational databasesLots of data is stored in relational databases

Should be able to access also themShould be able to access also them

Example: Tables for Parts and SuppliersExample: Tables for Parts and Suppliers– P (P (pnopno, , descripdescrip) : part numbers and descriptions) : part numbers and descriptions

– S (S (snosno, sname, sname) : supplier numbers and names) : supplier numbers and names

– SP (SP (snosno, , pnopno, , priceprice): ):

who supplies which parts and for what price?who supplies which parts and for what price?

SDPL 2007 7: Querying XML with XQuery 41

Possible XML representation of relationsPossible XML representation of relations

**

**

**

SDPL 2007 7: Querying XML with XQuery 42

Selecting in SQL vs. XQuery Selecting in SQL vs. XQuery

SQL:SQL:

XQuery:XQuery:

SELECTSELECT pnopnoFROMFROM ppWHEREWHERE descripdescrip LIKELIKE ’Gear%’’Gear%’ORDER BYORDER BY pnopno;;

forfor $p $p in in docdoc(”p.xml”)//p_tuple(”p.xml”)//p_tuplewhere starts-withwhere starts-with($p/descrip, ”Gear”)($p/descrip, ”Gear”)order by order by $p/pno $p/pno returnreturn $p/pno $p/pno

SDPL 2007 7: Querying XML with XQuery 43

GroupingGrouping

Many queries involve grouping data and applying Many queries involve grouping data and applying aggregation function like aggregation function like countcount or or avgavg to each to each groupgroup

in SQL: in SQL: GROUP BYGROUP BY and and HAVINGHAVING clauses clauses Example: Find the part number and average price Example: Find the part number and average price

for parts with at least 3 suppliersfor parts with at least 3 suppliers

SDPL 2007 7: Querying XML with XQuery 44

Grouping: SQLGrouping: SQL

SELECTSELECT pno, pno, avgavg(price) (price) ASAS avgprice avgprice

FROMFROM sp sp

GROUP BYGROUP BY pno pno

HAVING countHAVING count(*) >= 3(*) >= 3

ORDER BYORDER BY pno; pno;

SDPL 2007 7: Querying XML with XQuery 45

Grouping: XQueryGrouping: XQuery

forfor $pn $pn in distinct-values(in distinct-values(doc(doc(”sp.xml”)//pno)”sp.xml”)//pno)

letlet $sp:= $sp:=docdoc(”sp.xml”)//sp_tuple[pno=$pn](”sp.xml”)//sp_tuple[pno=$pn]

where countwhere count($sp) >= 3($sp) >= 3

order byorder by $pn $pn

returnreturn

<well_supplied_item> <well_supplied_item> {{

<pno>{$pn}</pno><pno>{$pn}</pno>,,

<avgprice> <avgprice> {avg{avg($sp/price)($sp/price)}} </avgprice> </avgprice>

}} <well_supplied_item> <well_supplied_item>

SDPL 2007 7: Querying XML with XQuery 46

JoinsJoins

Example: Return a ”flat” list of supplier names and their Example: Return a ”flat” list of supplier names and their part descriptions, in alphabetic orderpart descriptions, in alphabetic order

forfor $sp $sp in docin doc(”sp.xml”)//sp_tuple,(”sp.xml”)//sp_tuple, $p $p in docin doc(”p.xml”)//p_tuple[pno = $sp/pno],(”p.xml”)//p_tuple[pno = $sp/pno], $s $s in docin doc(”s.xml”)//s_tuple[sno = $sp/sno](”s.xml”)//s_tuple[sno = $sp/sno]order byorder by $p/descrip, $s/sname $p/descrip, $s/snamereturnreturn <sp_pair>{ <sp_pair>{

$s/sname ,$s/sname , $p/descrip$p/descrip

}<sp_pair>}<sp_pair>

SDPL 2007 7: Querying XML with XQuery 47

XQuery vs. XSLT 1.0XQuery vs. XSLT 1.0

Could we express XQuery queries with XSLT?Could we express XQuery queries with XSLT?– In principle In principle yes,yes, always, (but could be tedious) always, (but could be tedious)

Partial XSLT simulation of FLWOR expressions:Partial XSLT simulation of FLWOR expressions:

XQueryXQuery:: forfor $x$x inin Expr …rest of queryExpr …rest of query

can be expressed incan be expressed in XSLT XSLT asas::

<xsl: <xsl:for-each select="for-each select="ExprExpr">"> <xsl:<xsl:variable name="variable name="xx" select="." select="."" /> /> … … translation of the rest of the querytranslation of the rest of the query

</xsl:</xsl:for-each>for-each>

SDPL 2007 7: Querying XML with XQuery 48

XQuery vs. XSLT 1.0XQuery vs. XSLT 1.0

XQueryXQuery:: letlet $y :=$y := Expr …Expr …corresponds directly tocorresponds directly to:: <xsl: <xsl:variable name="variable name="yy" select="" select="ExprExpr" />" />

andand wherewhere Condition … restCondition … rest <xsl:<xsl:if test="if test="ConditionCondition">"> …… translation of the resttranslation of the rest </xsl:</xsl:if>if>

SDPL 2007 7: Querying XML with XQuery 49

XQuery vs. XSLT 1.0XQuery vs. XSLT 1.0

XQueryXQuery:: returnreturn ElemConstructorElemConstructorcan be simulated with a corresponding XSLT template:can be simulated with a corresponding XSLT template:

• static fragments as suchstatic fragments as such• enclosed expressions in element content, e.g. enclosed expressions in element content, e.g. {$s/sname}{$s/sname} becomebecome

<xsl:<xsl:copy-of select="copy-of select="$s/sname$s/sname" />" />

SDPL 2007 7: Querying XML with XQuery 50

XQuery vs. XSLT 1.0: ExampleXQuery vs. XSLT 1.0: Example

for for $b$b in in doc(”bib.xml”)//bookdoc(”bib.xml”)//book where where $b/publ = ”MK”$b/publ = ”MK” and and $b/year > 1998$b/year > 1998

return return <book year="{$b/year}"><book year="{$b/year}">

{$b/title} </book>{$b/title} </book>

XQueryXQuery::

<xsl:template match="/"><xsl:template match="/"> <xsl:for-each select=" <xsl:for-each select="document('bib.xml')//bookdocument('bib.xml')//book">"> <xsl:variable name="b" select="." /> <xsl:variable name="b" select="." /> <xsl:if test="$b/publ='MK' <xsl:if test="$b/publ='MK' and and $b/year > 1998”>$b/year > 1998”>

<book year="{$b/year}"><book year="{$b/year}">

<xsl:copy-of select="$b/title" /> </book><xsl:copy-of select="$b/title" /> </book> </xsl:if> </xsl:if> </xsl:for-each> </xsl:for-each> </xsl:template></xsl:template>

SDPL 2007 7: Querying XML with XQuery 51

XSLT for XQuery FLWOR ExpressionsXSLT for XQuery FLWOR Expressions

NB: The sketched simulation is not complete:NB: The sketched simulation is not complete:– Only two things, roughly, can be done with XSLT 1.0 Only two things, roughly, can be done with XSLT 1.0 result result

tree fragmentstree fragments produced by templates: produced by templates:» insertion in result treeinsertion in result tree

- with - with <xsl:copy-of select=”$X” /><xsl:copy-of select=”$X” /> and conversion to a string and conversion to a string

- with - with <xsl:value-of select=”$X” /><xsl:value-of select=”$X” />

– Not possible to apply other operations to results (like, e.g., Not possible to apply other operations to results (like, e.g., sorting in XQuery):sorting in XQuery):

forfor $y $y inin (<a><key>{$x/code}</key></a>, (<a><key>{$x/code}</key></a>, ......))

order byorder by $y/key $y/key

SDPL 2007 7: Querying XML with XQuery 52

XQuery: SummaryXQuery: Summary

– A recent W3C XML query language, also capable of A recent W3C XML query language, also capable of general XML processinggeneral XML processing

– Vendor support??Vendor support??» http://www.w3.org/XML/Queryhttp://www.w3.org/XML/Query

mentions ~ 50 prototypes or products (2004: ~ 30, 2005: ~ 40; mentions ~ 50 prototypes or products (2004: ~ 30, 2005: ~ 40; free, commercial, ... Oracle, IBM)free, commercial, ... Oracle, IBM)

– Future?? Interesting confluence of document and Future?? Interesting confluence of document and database research, and highly potential for XML-based database research, and highly potential for XML-based data integrationdata integration