Presentation is loading. Please wait.

Presentation is loading. Please wait.

SDPL 20077: Querying XML with XQuery1 7 Querying XML n How to access XML data sources? n XQuery, XML Query Lang, W3C Rec, 01/ '07 –joint work by XML Query.

Similar presentations


Presentation on theme: "SDPL 20077: Querying XML with XQuery1 7 Querying XML n How to access XML data sources? n XQuery, XML Query Lang, W3C Rec, 01/ '07 –joint work by XML Query."— Presentation transcript:

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

2 SDPL 20077: Querying XML with XQuery2 Functional Requirements (1) n Support operations (selection, projection, aggregation, sorting, etc.) on all data types: –Choose data based on content or structure –Operate on document hierarchy and order n Structural preservation and transformation: –Preserve relative hierarchy and sequence of input structures –Transform XML structures, and create new n Combining and joining: –Combine data from different parts of a document, or from multiple documents

3 SDPL 20077: Querying XML with XQuery3 Functional Requirements (2)  Closure property: –Results of XML queries are also XML (well-formed document fragments) –> queries can be combined without limit n Extensibility: –should support externally defined functions on all data types of the data model

4 SDPL 20077: Querying XML with XQuery4 XQuery in a Nutshell n Functional expression language n Strongly-typed: (optional) type-checking of expressions, and validation of results (We’ll concentrate to processing) –predeclared prefix for type names: xs=http://www.w3.org/2001/XMLSchema n Extends XPath 2.0 –XQuery 1.0 and XPath 2.0 Functions and Operators, Rec. Jan. 2007 n XQuery  XPath' + XSLT' + SQL' (roughly)

5 SDPL 20077: Querying XML with XQuery5 Example Query xquery version "1.0"; Cheap Books { for $b in fn:doc("bib.xml")//book[@price Cheap Books { for $b in fn:doc("bib.xml")//book[@price  XML-based syntax (XQueryX) also specified  Syntax "concise and easily understood"

6 SDPL 20077: Querying XML with XQuery6 A possible result Cheap Books Computing with Logic David Maier Benjamin Cummings 1999 Designing Internet applications Michael Leventhal Prentice Hall 1998 Cheap Books Computing with Logic David Maier Benjamin Cummings 1999 Designing Internet applications Michael Leventhal Prentice Hall 1998

7 SDPL 20077: Querying XML with XQuery7 XQuery: Basics n A query is an expression (lauseke) n Expressions operate on, and return sequences of –atomic values (of XML Schema simple types) and –nodes (of 6 types = XPath node types – NS nodes) –an item  a singleton sequence –sequences are flat: no sequences as items »(1, (2, 3), (), 1) = (1, 2, 3, 1) –sequences are ordered, and can contain duplicates n Unlimited combination of expressions, often with automatic type conversions (e.g. for arithmetics)

8 SDPL 20077: Querying XML with XQuery8 XQuery: Accessing Documents n XQuery operates on nodes accessible by input functions –fn:doc(" URI ") »document-node of the XML document available at URI »same as document(" URI ") in XSLT 1.0 –fn:collection(" URI ") »sequence of nodes from URI –predeclared prefix for the default function namespace: fn=http://www.w3.org/2005/04/xpath-functions

9 SDPL 20077: Querying XML with XQuery9 Central XQuery Expressions n Path expressions n Sequence expressions n Comparison operators n Quantified expressions (some/every $x… in … satisfies …) n Element constructors (  XSLT templates) n FLWOR expressions (”flower”; for-let-where-order by-return) n and others, in examples... also in XPath 2.0

10 SDPL 20077: Querying XML with XQuery10 Path Expressions n Similar to XPath 1.0: [/]Expr/… /Expr –but steps more liberal: –arbitrary (node-sequence valued) expressions OK –6 (of 13 XPath) axes: child, descendant, attribute, self, descendant-or-self, parent »others (except namespace ) available if Full Axis Feature supported »with document-order operators ( >) sufficient for expressing queries (→ Exercises) –produce a sequence of nodes in document order, without duplicates

11 SDPL 20077: Querying XML with XQuery11 Sequence Expressions n Constant sequences constructed by listing values –comma (, ) is a catenation operator »(1, (2, 3), (), 1) = (1, 2, 3, 1) n Also dynamically generated: –(, $emp/child, ) n Shorthands for numeric sequences: –1 to 4 –4 to 1 –fn:reverse(1 to 4)  (1, 2, 3, 4)  ()  (4, 3, 2, 1)

12 SDPL 20077: Querying XML with XQuery12 Set Operations on Node (!) Sequences n Assume variable bindings: $s2 n Then: $s1 $s1 union $s2 = $s1 intersect $s2 = $s1 except $s2 = based on node indentity ( node 1 is node 2 ) abcde abcde c ab

13 SDPL 20077: Querying XML with XQuery13 Node Comparisons n To compare single nodes, –for identity: is $book//chap[@id="ch1"] is ($book//chap)[1] true iff the chapter with id="ch1" is indeed the first –for document order: > $book//chap[@id="ch2"] >> $book//title[. eq "Intro"] true iff the chapter with id="ch2" appears after Intro $book//chap[@id="ch2"] >> $book//title[. eq "Intro"] true iff the chapter with id="ch2" appears after Intro

14 SDPL 20077: Querying XML with XQuery14 Comparing values of sequences and items n General comparisons btw sequences: –=, !=,, >= –existential semantics: true iff some pair of values from operand sequences satisfy the condition »(1,2) = (2,3); (2,3) = (3,4); (1,2) != (3,4) »Same as in XPath 1.0: //book[author = "Aho"] → books where some author is Aho n Value comparisons btw single values: –eq, ne, lt, le, gt, ge »1 eq 3 - 2; 10 lt 20; $books[@price le 100]

15 SDPL 20077: Querying XML with XQuery15 Filter Expressions Location steps can be filtered by predicates: $book//(chap | app)[fn:last()]/title Location steps can be filtered by predicates: $book//(chap | app)[fn:last()]/title the title of the last chapter of appendix, whichever is last the title of the last chapter of appendix, whichever is last Other sequences, too: (1 to 20)[. mod 5 eq 0] → (5, 10, 15, 20) Other sequences, too: (1 to 20)[. mod 5 eq 0] → (5, 10, 15, 20) –('. ' generalized from XPath 1.0 shorthand for self::node() into the context item) XPath 2.0 extended step

16 SDPL 20077: Querying XML with XQuery16 Element Constructors n Similar to XSLT templates: –start and end tag enclosing the content –literal fragments written directly, expressions enclosed in braces { and } ≈ XSLT 1.0 attribute value templates n often used inside another expression that binds variables used in the element constructor –(There is no 'current node' in XQuery) –See next

17 SDPL 20077: Querying XML with XQuery17 Example An emp element with an empid attribute and child elements name and job, from values in variables $id, $n, and $j : An emp element with an empid attribute and child elements name and job, from values in variables $id, $n, and $j : {$n} {$n} {$j} {$j} </emp> Also computed constructors: element {"emp"} { attribute {"empid"}{$id}, {$n}, {$j} }

18 SDPL 20077: Querying XML with XQuery18 FLWOR ("flower") Expressions n Constructed from for, let, where, order by and return clauses (~SQL select-from-where) n Syntax: (ForClause | LetClause)+ WhereClause? OrderByClause? "return" Expr n FLWOR binds variables to values, and uses these bindings to construct a result (an ordered sequence of items)

19 SDPL 20077: Querying XML with XQuery19 Flow of data in a FLWOR expression

20 SDPL 20077: Querying XML with XQuery20 for clauses for $V 1 in Exp 1 (, $V 2 in Exp 2, …) for $V 1 in Exp 1 (, $V 2 in Exp 2, …) –associates each variable V i with expression Exp i (e.g. a path expression) n Result: list of tuples, each containing a binding for each of the variables n can be though of as loops iterating over the items returned by respective expressions

21 SDPL 20077: Querying XML with XQuery21 Example: for clause for $i in (1,2), $j in (1 to $i) return {$i} {$j} for $i in (1,2), $j in (1 to $i) return {$i} {$j} Result:<tuple><i>1</i><j>1</j></tuple><tuple><i>2</i><j>1</j></tuple><tuple><i>2</i><j>2</j></tuple>

22 SDPL 20077: Querying XML with XQuery22 let clauses n let also binds variables to expressions –each variable gets the entire sequence as its value (without iterating over the items of the sequence) –results in binding a single sequence for each variable n Compare: –for $b in doc("bib.xml")//book  many bindings (to single books) –let $bl := doc("bib.xml")//book  a single binding (to sequence of books)

23 SDPL 20077: Querying XML with XQuery23 Example: let clauses let $s := (,, ) return {$s} return {$s} Result:<out> </out> for $s in (,, ) return {$s} return {$s} --> -->

24 SDPL 20077: Querying XML with XQuery24 for/let clauses n A FLWOR expr may contain several fors and lets –each may refer to variables bound in previous clauses n the result of the for/let sequence: –an ordered list of tuples (monikko) of bound variables –number of tuples = product of the cardinalities of the sequences returned by the for expressions

25 SDPL 20077: Querying XML with XQuery25 where clause n binding tuples generated by for and let clauses are filtered by an optional where clause –tuples with a true condition are used to instantiate the return clause the where clause may contain several predicates connected by and, or, and fn:not() the where clause may contain several predicates connected by and, or, and fn:not() –usually refer to the bound variables –sequences as Booleans (similarly to node-sets in XPath 1.0): empty ~ false; non-empty ~ true

26 SDPL 20077: Querying XML with XQuery26 where clause n for binds variables to single items  value comparisons, e.g. $color eq " red "  value comparisons, e.g. $color eq " red " let to whole sequences  general comparisons, e.g. $colors = "red" ( ~ some $c in $colors satisfies $c eq "red" ) let to whole sequences  general comparisons, e.g. $colors = "red" ( ~ some $c in $colors satisfies $c eq "red" ) –a number of aggregation functions available: avg(), sum(), count(), max(), min() (also in XPath 1.0)

27 SDPL 20077: Querying XML with XQuery27 return clause n The return clause generates the output of the FLWOR expression n instantiated once for each binding tuple n often contains element constuctors, references to bound variables, and nested sub-expressions

28 SDPL 20077: Querying XML with XQuery28 Example: for + return for $i in (1,2), $j in (1 to $i) return {$i} {$j} for $i in (1,2), $j in (1 to $i) return {$i} {$j} Result:<tuple><i>1</i><j>1</j></tuple><tuple><i>2</i><j>1</j></tuple><tuple><i>2</i><j>2</j></tuple>

29 SDPL 20077: Querying XML with XQuery29 Examples (modified from "XML Query Use Cases") Assume: a document named ” bib.xml ” containing of a list of book s: + + Assume: a document named ” bib.xml ” containing of a list of book s: + +

30 SDPL 20077: Querying XML with XQuery30 { { } } List Morgan Kaufmann book titles since 1998 for $b in doc(”bib.xml”)//book where $b/publisher = ”Morgan Kaufmann” and $b/year >= 1998 and $b/year >= 1998 return return {$b/title}

31 SDPL 20077: Querying XML with XQuery31 Result could be... <recent-MK-books> TCP/IP Illustrated TCP/IP Illustrated Advanced Programming in the Unix environment Advanced Programming in the Unix environment </recent-MK-books>

32 SDPL 20077: Querying XML with XQuery32 Publishers with avg price of their books: for $p in fn:distinct-values( fn:doc(”bib.xml”)//publisher ) let $a := avg( doc(”bib.xml”)//book[ publisher = $p]/price ) return {$p} {$a} return {$p} {$a} string values of the sequence, without duplicates

33 SDPL 20077: Querying XML with XQuery33 Invert the book-list structure { (: group books by authors :) { (: group books by authors :) for $a in distinct-values( doc(”bib.xml”)//author ) for $a in distinct-values( doc(”bib.xml”)//author ) return { return { {$a}, {$a}, for $b in doc(”bib.xml”)//book[ for $b in doc(”bib.xml”)//book[ author = $a] return $b/title } return $b/title } } }

34 SDPL 20077: Querying XML with XQuery34 List of publishers alphabetically, and their books in descending order of price for $p in distinct-values( doc(”bib.xml”)//publisher ) order by $p return {$p} { for $b in doc(”bib.xml”)//book[ {$p} { for $b in doc(”bib.xml”)//book[ publisher = $p] order by $b/price descending return {$b/title, return {$b/title, $b/price} } $b/price} }

35 SDPL 20077: Querying XML with XQuery35 Queries on Document Order n Operators >: –x > x similarly) n Consider a surgical report with –procedure elements containing »incision sub- elements n Return a "critical sequence" of contents between the first and the second incisions of the first procedure

36 SDPL 20077: Querying XML with XQuery36 Computing a "critical sequence" { { let $p := (doc("report.xml")//procedure)[1] let $p := (doc("report.xml")//procedure)[1] for $n in $p/node() for $n in $p/node() where $n >> ($p//incision)[1] and $n > ($p//incision)[1] and $n << ($p//incision)[2] return $n } return $n } NB: if incision s are not children of the procedure, then an ancestor of the second incision gets to the result; How to avoid this? NB: if incision s are not children of the procedure, then an ancestor of the second incision gets to the result; How to avoid this?

37 SDPL 20077: Querying XML with XQuery37 User-defined functions: Example declare function local:precedes($a as node(), $b as node()) as xs:boolean { $a << $b and (: $a is no ancestor of $b: :) empty($a//node() intersect $b) }; empty($a//node() intersect $b) }; local : is predeclared prefix for the namespace of local function names local : is predeclared prefix for the namespace of local function names –Alternatively: declare namespace my=http://my.namespace.org; declare namespace my=http://my.namespace.org; declare function my:precedes(... (as above)

38 SDPL 20077: Querying XML with XQuery38 User-defined functions: Example Now, ”critical sequence” without ancestors of incision : Now, ”critical sequence” without ancestors of incision : { { let $p := (doc("report.xml")//procedure)[1] let $p := (doc("report.xml")//procedure)[1] for $n in $p/node() for $n in $p/node() where $n >> ($p//incision)[1] and local:precedes($n, ($p//incision)[2]) where $n >> ($p//incision)[1] and local:precedes($n, ($p//incision)[2]) return $n return $n } }

39 SDPL 20077: Querying XML with XQuery39 Recursive Transformations n Example: “Table-of-contents” for nested sections –NB if-then-else (in ordinary XPath 2.0 expressions, too) declare namespace my=http://my.own-ns.org; declare function my:toc( $n as element() ) as element()* { if (name($n)=”sect”) then { then { for $c in $n/* return my:toc($c) } for $c in $n/* return my:toc($c) } else if (name($n)=”title”) then $n else if (name($n)=”title”) then $n else (: check child elements, if any: :) for $c in $n/* return my:toc($c) }; else (: check child elements, if any: :) for $c in $n/* return my:toc($c) };

40 SDPL 20077: Querying XML with XQuery40 Querying relational data n Lots of data is stored in relational databases n Should be able to access also them n Example: Tables for Parts and Suppliers –P ( pno, descrip ) : part numbers and descriptions –S ( sno, sname ) : supplier numbers and names –SP ( sno, pno, price ): who supplies which parts and for what price?

41 SDPL 20077: Querying XML with XQuery41 Possible XML representation of relations * * *

42 SDPL 20077: Querying XML with XQuery42 Selecting in SQL vs. XQuery n SQL: n XQuery: SELECT pno FROM p WHERE descrip LIKE ’Gear%’ ORDER BY pno; for $p in doc(”p.xml”)//p_tuple where starts-with($p/descrip, ”Gear”) order by $p/pno return $p/pno

43 SDPL 20077: Querying XML with XQuery43 Grouping n Many queries involve grouping data and applying aggregation function like count or avg to each group n in SQL: GROUP BY and HAVING clauses n Example: Find the part number and average price for parts with at least 3 suppliers

44 SDPL 20077: Querying XML with XQuery44 Grouping: SQL SELECT pno, avg(price) AS avgprice FROM sp GROUP BY pno HAVING count(*) >= 3 ORDER BY pno;

45 SDPL 20077: Querying XML with XQuery45 Grouping: XQuery for $pn in distinct-values( doc(”sp.xml”)//pno) let $sp:=doc(”sp.xml”)//sp_tuple[pno=$pn] where count($sp) >= 3 order by $pn return { { {$pn}, {$pn}, {avg($sp/price)} {avg($sp/price)} } }

46 SDPL 20077: Querying XML with XQuery46 Joins n Example: Return a ”flat” list of supplier names and their part descriptions, in alphabetic order for $sp in doc(”sp.xml”)//sp_tuple, $p in doc(”p.xml”)//p_tuple[pno = $sp/pno], $p in doc(”p.xml”)//p_tuple[pno = $sp/pno], $s in doc(”s.xml”)//s_tuple[sno = $sp/sno] $s in doc(”s.xml”)//s_tuple[sno = $sp/sno] order by $p/descrip, $s/sname return { $s/sname, $s/sname, $p/descrip $p/descrip } }

47 SDPL 20077: Querying XML with XQuery47 XQuery vs. XSLT 1.0 n Could we express XQuery queries with XSLT? –In principle yes, always, (but could be tedious) n Partial XSLT simulation of FLWOR expressions: XQuery : for $x in Expr …rest of query can be expressed in XSLT as : can be expressed in XSLT as : … translation of the rest of the query … translation of the rest of the query

48 SDPL 20077: Querying XML with XQuery48 XQuery vs. XSLT 1.0 XQuery : let $y := Expr … corresponds directly to : corresponds directly to : and where Condition … rest  … translation of the rest … translation of the rest

49 SDPL 20077: Querying XML with XQuery49 XQuery vs. XSLT 1.0 XQuery : return ElemConstructor can be simulated with a corresponding XSLT template: static fragments as such static fragments as such enclosed expressions in element content, e.g. {$s/sname} become enclosed expressions in element content, e.g. {$s/sname} become

50 SDPL 20077: Querying XML with XQuery50 XQuery vs. XSLT 1.0: Example for $b in doc(”bib.xml”)//book where $b/publ = ”MK” and $b/year > 1998 return return {$b/title} {$b/title} XQuery: 1998”> 1998”> 

51 SDPL 20077: Querying XML with XQuery51 XSLT for XQuery FLWOR Expressions n NB: The sketched simulation is not complete: –Only two things, roughly, can be done with XSLT 1.0 result tree fragments produced by templates: »insertion in result tree - with and conversion to a string - with »insertion in result tree - with and conversion to a string - with –Not possible to apply other operations to results (like, e.g., sorting in XQuery): for $y in ( {$x/code},... ) order by $y/key

52 SDPL 20077: Querying XML with XQuery52 XQuery: Summary –A recent W3C XML query language, also capable of general XML processing –Vendor support?? »http://www.w3.org/XML/Query mentions ~ 50 prototypes or products (2004: ~ 30, 2005: ~ 40; free, commercial,... Oracle, IBM) –Future?? Interesting confluence of document and database research, and highly potential for XML-based data integration


Download ppt "SDPL 20077: Querying XML with XQuery1 7 Querying XML n How to access XML data sources? n XQuery, XML Query Lang, W3C Rec, 01/ '07 –joint work by XML Query."

Similar presentations


Ads by Google