1 / 45

XQuery language

XQuery language . Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti. Why XQuery ??. Other query languages are too specific XML covers many facets of data modeling Need a query language to reflect that. Why not other languages ??. Document-oriented XQL, XPath, XSLT Relational SQL

moe
Download Presentation

XQuery language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XQuery language Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti

  2. Why XQuery ?? • Other query languages are too specific • XML covers many facets of data modeling • Need a query language to reflect that

  3. Why not other languages ?? • Document-oriented • XQL, XPath, XSLT • Relational • SQL • Object • OQL • Semi-structured • XML-QL, Lorel, YATL, UnQL

  4. Who made it ?? • Quilt • 3 people • World Wide Web Consortium (W3C) • Query Language Working Group • XML Query Working Group

  5. Who (cont.) ?? • 5 stages to W3C “Recommendation” • Working Draft • Last Call Working Draft • Candidate Recommendation • Proposed Recommendation • W3C Recommendation

  6. When/Where ?? • W3C Query Language Workshop: Boston, December 1998 • First Draft: April 2001 • Working Draft: Draft 7, June 2001 • Final: ?

  7. Origins XQL SQL Xpointer XSL patterns OQL XML - QL Xpath XQL - 99 Quilt XQuery

  8. XML Query Requirements General: • Declarative Language • Readable XML Syntax • Protocol Independence • Standard Error Conditions • Support for Future Updates Data Model: • Based on XML Infosets • Namespace Aware • Support for XML Schema Data Types • Support Inter/Intra Document References

  9. XML Query Requirements (cont.) Query Functionality: • Operators on All Data Types • Text Operators Across Element Boundaries • Hierarchies and Sequences • Combination of Data from Various Locations • Aggregation and Sorting • Combination of Operators (Queries as Operands) • Support NULL values • Preservation of Structure/Identity • Operations on Names/Schemas • Extensibility & Closure

  10. What is XQuery • A language for querying XML • A human-readable syntax for an XML query language • A functional programming language • The product of the W3C’s XML Query Working Group

  11. XQuery • Functional Language • Query Represented as an Expression • Expressions can be Nested without Restriction • Input/Output of an XQuery are Instances of the XML • Query Data Model • Based on OQL, SQL, XML-QL, XPath • Readable XML Syntax

  12. XQuery 1.0 & XPath 2.0 XQueryX 1.0 XQuery 1.0 XPath 2.0 XQuery 1.0 & Xpath 2.0Formal Semantics XQuery 1.0 & Xpath 2.0Functions & Operators XQuery 1.0 & Xpath 2.0Data Model XQuery 1.0 & Xpath 2.0Requirements & Use Cases XML Schema 1.0 XML 1.0 Namespaces 1.0

  13. . Denotes the current node. .. Denotes the parent of the current node. / Denotes the root node, or a separator between steps in a path. // Denotes descendants of the current node. @ Denotes attributes of the current node. * Denotes "any" (node with unrestricted name). [ ] Brackets enclose a Boolean expression that serves as a predicate for a given step. [n] When a predicate consists of an integer, it serves to select the element with the given ordinal number from a list of elements. Symbols

  14. XQuery Expressions • Path Expressions • Element Constructors • FLWR Expressions • Expressions with Operators/Functions • Conditional Expressions • Quantified Expressions

  15. Path Expression (Q) In the second chapter of the document named "zoo.xml", find the figure(s) with caption "Tree Frogs". document("zoo.xml")//chapter[2]//figure[caption = "Tree Frogs"] • First step locates the root node of a document. • Second step locates the second chapter of the document (ordinal number) • Third step finds figure elements occurring anywhere within the chapter, but retains those figure elements that have a caption with the value "Tree Frogs."

  16. XQuery Element Constructor • Start/End Tag + Enclosed List of Expressions • Generate an element with a computed name that contains nested elements: <$tagname> <description> $d </description> <price> $p </price> </$tagname>

  17. XQuery For Let Where Return (FLWR) • FOR and LET Clause • Generate a List of Tuples that Preserves Doc Order • WHERE Clause • Applies a Predicate to Eliminate Some Tuples • RETURN Clause • Executed on Resulting Tuples -> Ordered Output List • Syntax: FOR var IN expr WHERE expr RETURN expr LET var := expr

  18. FLWR Expressions • A FLWR (pronounced "flower") expression is constructed from FOR, LET, WHERE, and RETURN clauses, which must appear in a specific order. • A FLWR expression binds values to one or more variables and then uses these variables to construct a result.

  19. XML Document • <bib> • <book year="1994"> <title>TCP/IP Illustrated</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher> • <price> 65.95</price> </book> • <book year="1992"> <title>Advanced Programming in the Unix environment</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher> • <price>65.95</price> </book> • <book year="1999"> • <title>The Economics of Technology and Content for Digital TV</title> <editor> <last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor> • <publisher>Kluwer Academic Publishers</publisher> <price>129.95</price> • </book> • </bib>

  20. FLWR Expressions • (Q) List each publisher and the average price of its books. FOR $p IN distinct(document("bib.xml")//publisher) LET $a := avg(document("bib.xml")//book[publisher = $p]/price) RETURN <publisher> <name> {$p/text()} </name> <avgprice> {$a} </avgprice> </publisher> distinct = a function that eliminates duplicates

  21. XQuery Operators and Functions • Infix/Prefix Operators • e.g., Infix Operators BEFORE and AFTER • Parenthesized Expressions • Arithmetic/Logical Operators • Collection Operators • e.g., UNION, INTERSECT, EXCEPT • Functions Can Be Defined in XQuery

  22. Conditional Expressions (Q) Make a list of holdings, ordered by title. For journals, include the editor, and for all other holdings, include the author. FOR $h IN //holding RETURN <holding> {$h/title, IF ($h/@type = "Journal") THEN $h/editor ELSE $h/author } </holding> SORTBY (title)

  23. If-Then-Else FOR$h IN //holding RETURN <holding> $h/title, IF$h/@type = "Journal" THEN$h/editor ELSE$h/author </holding> SORTBY (title)

  24. Quantifiers • Tests for existence of some elements that satisfy a condition • Also used to test whether all elements in a collection satisfy a condition • Key words satisfies and contains

  25. XQuery Quantified Expressions (Q) Find titles of books in which both sailing and windsurfing are mentioned in the same paragraph. FOR $b IN //book WHERE SOME $p IN $b//para SATISFIES (contains($p, "sailing") AND contains($p, "windsurfing")) RETURN $b/title

  26. Sorting • A sequence can be ordered by means of a SORTBY clause that contains one or more "ordering expressions." (Q)List all books with price greater than $100, in order by first author; within each group of books with the same first author, list the books in order by title. document("bib.xml")//book[price > 100] SORTBY (author[1], title)

  27. Sorting in XQuery <publisher_list> FOR$pINdistinct(document("bib.xml")//publisher) RETURN <publisher> <name> $p/text() </name> , FOR$bIN document("bib.xml")//book[publisher = $p] RETURN <book> $b/title , $b/@price </book> SORTBY(price DESCENDING) </publisher> SORTBY(name) </publisher_list>

  28. Collections in XQuery • Ordered and unordered collections • /bib/book/author = an ordered collection • Distinct(/bib/book/author) = an unordered collection • LET$a = /bib/book $a is a collection • $b/author  a collection (several authors...) Returns: <result> <author>...</author> <author>...</author> <author>...</author> ... </result> RETURN <result> $b/author </result>

  29. Collections in XQuery What about collections in expressions ?$b/@price list of n prices$b/@price * 0.7  list of n numbers$b/@price * $b/@quantity  list of n x m numbers ??$b/@price * ($b/@quant1 + $b/@quant2)  $b/@price * $b/@quant1 + $b/@price * $b/@quant2 !!

  30. Group-By in Xquery ?? FOR$bIN document("http://www.bn.com")/bib/book, $yIN$b/@year WHERE$b/publisher="Morgan Kaufmann" RETURNGROUPBY $y WHERE count($b) > 10 IN <year> $y </year>  with GROUPBY count = a (aggregate) function that returns the number of elms SELECT year FROM Bib WHERE Bib.publisher="Morgan Kaufmann" GROUPBY year HAVING count(*) > 10 Equivalent SQL 

  31. JOINS in Relation P (part) P_no descrip qnty S (supplier) s_no name locat SP (Supplies) p_no s_no price

  32. XML documents P.XML <parts> <p_tuple> <p_no> 1 </p_no> <descrip> ABC </descrip> <qty> 100 </qty> </p_tuple> </parts> S.XML <supplier> <s_tuple> <s_no> 27 </s_no> <name> IBM </name> <locat> NY </locat> </s_tuple> </supplier> SP.XML <supplies_part> <sp_tuple> <p_no> 2 </p_no> <s_no> 24 </s_no> <price> 5.00 </price> </sp_tuple> </supplies_part>

  33. JOINS in XQuery For $sp in document(“sp.xml”)//sp_tuple, $p in document(“p.xml”)//p_tuple[ p_no = $sp/p_no] $s in document(“s.xml”)//s_tuple [s_no = $sp/s_no] Return <sp_pair> { $s/name, $p/descrip } </sp_pair> sortby(name, descrip)

  34. Additional Issues • Updates : not implemented • Case sensitivity: all keywords are case sensitive • Type checking: When being processed a query goes through two phases: Analysis Phase & Evaluation Phase. • During the Analysis phase type checking occurs to provide early detection of type errors and to compute the type of the result. • Querying XML documents with cycles

  35. <bib> <book year="1994"> <title>TCP/IP Illustrated</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher> <price> 65.95</price> </book> <book year="1992"> <title>Advanced Programming in the Unix environment</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="1999"> <title>The Economics of Technology and Content for Digital TV</title> <editor> <last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor> <publisher>Kluwer Academic Publishers</publisher> <price>129.95</price> </book> </bib> Xml document

  36. List books published by Addison-Wesley after 1991, including their year and title. <bib> { for $b in document("http://www.bn.com")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book year="{ $b/@year }"> { $b/title } </book> } </bib> Xquery example 1

  37. Expected Result • <bib> • <book year="1994"> • <title>TCP/IP Illustrated</title> • </book> • <book year="1992"> • <title>Advanced Programming in the Unix environment</title> • </book> • </bib>

  38. Create a flat list of all the title-author pairs, with each pair enclosed in a "result" element. <results> { for $b in document("http://www.bn.com")/bib/book, $t in $b/title, $a in $b/author return <result> { $t } { $a } </result> } </results> XQuery Example 2

  39. <results> <result> <title>TCP/IP Illustrated</title> <author> <last>Stevens</last> <first>W.</first> </author> Expected Results

  40. For each book found at both bn.com and amazon.com, list the title of the book and its price from each source: <books-with-prices> { for $b in document("www.bn.com/bib.xml")//book, $a in document("www.amazon.com/reviews.xml")//entry where $b/title = $a/title return <book-with-prices> { $b/title } <price-amazon>{ $a/price/text() }</price-amazon> <price-bn>{ $b/price/text() }</price-bn> </book-with-prices> } </books-with-prices> XQuery example 3

  41. For each book that has at least one author, list the title and first two authors, and an empty "et-al" element if the book has additional authors. <bib> { for $b in document("www.bn.com/bib.xml")//book where count($b/author) > 0 return <book> { $b/title } { for $a in $b/author[position()<=2] return $a } { if (count($b/author) > 2) then <et-al/> else () } </book> } </bib> XQuery Example 4

  42. <book> <title>Data on the Web</title> <author> <last>Abiteboul</last> <first>Serge</first> </author> <author> <last>Buneman</last> <first>Peter</first> </author> <et-al/> </book> Result

  43. Xquery example 5 • List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order. • <bib> { for $b in document("www.bn.com/bib.xml")//book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book> { $b/@year } { $b/title } </book> sort by (title) } </bib>

  44. References • W3C XQuery http://www.w3.org/TR/xquery.html • W3C XML Query Use Cases http://www.w3.org/TR/xmlquery-use-cases.html • W3C XML Query Requirements http://www.w3.org/TR/xmlquery-req.html • W3C XML Query Data Model http://www.w3.org/TR/query-datamodel.html • W3C XML Query Algebra http://www.w3.org/TR/query-algebra.html

  45. Q/A Session Your questions and feedback are most welcome......

More Related