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

52
SDPL 2006 7: Querying XML with XQuer y 1 7 Querying XML 7 Querying XML How to access data sources as XML? How to access data sources as XML? XQuery XQuery , W3C XML Query Language , W3C XML Query Language Candidate Rec, November 2005 Candidate Rec, November 2005 joint work by XML Query and XSL Working Groups joint work by XML Query and XSL Working Groups » 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, Lorel, ... , XQL, XML-QL, SQL, OQL, Lorel, ... Goal: a query language for any XML- Goal: a query language for any XML- represented data: both documents and databases represented data: both documents and databases

Upload: godfrey-willis

Post on 16-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 1

7 Querying XML7 Querying XML

How to access data sources as XML?How to access data sources as XML? XQueryXQuery, W3C XML Query Language, W3C XML Query Language

– Candidate Rec, November 2005Candidate Rec, November 2005– joint work by XML Query and XSL Working Groupsjoint work by XML Query and XSL Working Groups

» with XPath 2.0 and XSLT 2.0with XPath 2.0 and XSLT 2.0– influenced by many research groups and query influenced by many research groups and query

languageslanguages» QuiltQuilt, , XPathXPath, XQL, XML-QL, SQL, OQL, Lorel, ..., XQL, XML-QL, SQL, OQL, Lorel, ...

– Goal: a query language for any XML-represented Goal: a query language for any XML-represented data: both documents and databasesdata: both documents and databases

Page 2: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 parts of data based on content or structureChoose parts of data based on content or structure– Also operations on document hierarchy and orderAlso operations on document hierarchy and order

Structural preservation and transformation:Structural preservation and transformation: – Preserve the relative hierarchy and sequence of input document Preserve the relative hierarchy and sequence of input document

structures in the query resultsstructures in the query results– Transform XML structures and create new XML structuresTransform XML structures and create new XML structures

Combining and joining:Combining and joining:– Combine related information from different parts of a given Combine related information from different parts of a given

document or from multiple documentsdocument or from multiple documents

Page 3: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 3

Functional Requirements Functional Requirements (2)(2)

Closure property:Closure property: – The result of an XML query is also XML (usually not a The result of an XML query is also XML (usually not a

valid document, but a well-formed document valid document, but a well-formed document fragmentfragment))– > results can be used as an input to another query> results can be used as an input to another query

Cross-references Cross-references – ability to traverse intra- and inter-document references ability to traverse intra- and inter-document references

Extensibility:Extensibility:– The query language should support the use of externally The query language should support the use of externally

defined functions on all data types of the data modeldefined functions on all data types of the data model

Page 4: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 4

XQuery in a NutshellXQuery in a Nutshell

Functional expression languageFunctional expression language Strongly-typedStrongly-typed: : expressions can be (optionally) expressions can be (optionally) type-checkedtype-checked, ,

and results and results validatedvalidated (We’ll mainly skip these, and concentrate (We’ll mainly skip these, and concentrate on on processingprocessing with XQuery) with XQuery)– 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 ((but support for all axesbut support for all axes is optional!) is optional!)

– common for common for XQuery 1.0 and XPath 2.0XQuery 1.0 and XPath 2.0» Functions and OperatorsFunctions and Operators, Cand. Rec, Nov 2005, Cand. Rec, Nov 2005

Roughly: XQuery Roughly: XQuery XPath' + XSLT' + SQL' XPath' + XSLT' + SQL'

Page 5: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

A processor-friendly XML-based syntax A processor-friendly XML-based syntax (XQueryX) is also being developed(XQueryX) is also being developed

Page 6: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 7: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 7

XQuery: BasicsXQuery: Basics

A query is an A query is an expressionexpression 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 node types = XPath 1.0 (of 6 node types = XPath 1.0 –– 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)

Page 8: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 9: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 9

Central XQuery ExpressionsCentral XQuery Expressions

Path expressionsPath expressions Sequence expressions Sequence expressions Comparison operatorsComparison operators Element constructors (Element constructors ( XSLT templates) XSLT templates) FLWOR expressions FLWOR expressions

(”flower”; (”flower”; fforor--lletet--wwherehere--oorder byrder by--rreturneturn)) Quantified expressions Quantified expressions

((somesome//everyevery $x… $x… in … satisfiesin … satisfies …) …) and others in examples ... and others in examples ...

Page 10: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 10

Path ExpressionsPath Expressions

Similar to XPath: Similar to XPath: [/]Expr/… /Expr[/]Expr/… /Expr

– arbitrary (node-sequence-valued) expressions as stepsarbitrary (node-sequence-valued) expressions as steps– 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 an optional ) available if an optional Full Axis Full Axis

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

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

– produce an ordered sequence of nodes in document order, produce an ordered sequence of nodes in document order, without duplicateswithout duplicates

Page 11: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 11

Sequence ExpressionsSequence Expressions

Constant sequences constructed by listing Constant sequences constructed by listing valuesvalues– comma (comma (,,) used as catenation operator) used as 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)

Page 12: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 13: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 13

Comparison OperatorsComparison Operators

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]

Page 14: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 14

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>

Page 15: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 16: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 }}» cf. XSLT 1.0 attribute value templates cf. 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

Page 17: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 with Also with computedcomputed names: names:

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

Page 18: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 nodes)(an ordered sequence of nodes)

Page 19: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 19

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

Page 20: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 21: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 22: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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)

Page 23: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 24: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

node-lists returned by the expressions in the node-lists returned by the expressions in the forfor clausesclauses

Page 25: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 treated as Booleans (similarly to node-sets sequences treated as Booleans (similarly to node-sets in XPath 1.0): empty ~ in XPath 1.0): empty ~ falsefalse; non-empty ~ ; non-empty ~ truetrue

Page 26: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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)

Page 27: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 subexpressionsbound variables, and nested subexpressions

Page 28: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 29: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 30: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 31: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 32: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 33: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 34: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 35: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 36: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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?

Page 37: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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)) }; };

Here Here locallocal:: is predeclared prefix for a namespace of is predeclared prefix for a namespace of local function nameslocal function 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)

Page 38: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 39: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 expressions, too)(in ordinary 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) };

Page 40: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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?

Page 41: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 41

Possible XML representation of relationsPossible XML representation of relations

**

**

**

Page 42: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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 pno; pno;

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

Page 43: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 44: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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;

Page 45: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 46: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 47: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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)

Ex: Partial XSLT simulation of FLWOR expressionsEx: 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>

Page 48: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 49: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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" />" />

Page 50: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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>

Page 51: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 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

Page 52: SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005

SDPL 2006 7: Querying XML with XQuery 52

XQuery: SummaryXQuery: Summary

– W3C XML query language (draft), also capable of W3C XML query language (draft), also capable of general XML processinggeneral XML processing

– Recommendation expected within one year(?)Recommendation expected within one year(?)– Vendor support??Vendor support??

» http://www.w3.org/XML/Queryhttp://www.w3.org/XML/Query mentions about 50 prototypes or products mentions about 50 prototypes or products (2004: ~30, 2005: ~40)(2004: ~30, 2005: ~40)

– 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