Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "XQuery language Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti."— 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 SQL OQL XQL Xpointer Xpath XQL - 99 XML - QL XSL patterns 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 XML 1.0Namespaces 1.0 XML Schema 1.0 XQuery 1.0 & Xpath 2.0 Data Model XQuery 1.0 & Xpath 2.0 Functions & Operators XQuery 1.0 & Xpath 2.0 Formal Semantics XQuery 1.0 & Xpath 2.0 Requirements & Use Cases XQuery 1.0XPath 2.0 XQueryX 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: $d $p

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 TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in the Unix environment Stevens W. Addison-Wesley 65.95 The Economics of Technology and Content for Digital TV Gerbarg Darcy CITI Kluwer Academic Publishers 129.95

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 {$p/text()} {$a} 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 {$h/title, IF ($h/@type = "Journal") THEN $h/editor ELSE $h/author } SORTBY (title)

23 If-Then-Else FOR $h IN //holding RETURN $h/title, IF $h/@type = "Journal" THEN $h/editor ELSE $h/author SORTBY (title) FOR $h IN //holding RETURN $h/title, IF $h/@type = "Journal" THEN $h/editor ELSE $h/author 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 FOR $p IN distinct(document("bib.xml")//publisher) RETURN $p/text(), FOR $b IN document("bib.xml")//book[publisher = $p] RETURN $b/title, $b/@price SORTBY(price DESCENDING) SORTBY(name) FOR $p IN distinct(document("bib.xml")//publisher) RETURN $p/text(), FOR $b IN document("bib.xml")//book[publisher = $p] RETURN $b/title, $b/@price SORTBY(price DESCENDING) SORTBY(name)

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...) RETURN $b/author Returns:...

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 $b IN document("http://www.bn.com")/bib/book, $y IN $b/@year WHERE $b/publisher="Morgan Kaufmann" RETURN GROUPBY $y WHERE count($b) > 10 IN $y FOR $b IN document("http://www.bn.com")/bib/book, $y IN $b/@year WHERE $b/publisher="Morgan Kaufmann" RETURN GROUPBY $y WHERE count($b) > 10 IN $y SELECT year FROM Bib WHERE Bib.publisher="Morgan Kaufmann" GROUPBY year HAVING count(*) > 10 SELECT year FROM Bib WHERE Bib.publisher="Morgan Kaufmann" GROUPBY year HAVING count(*) > 10  with GROUPBY Equivalent SQL  count = a (aggregate) function that returns the number of elms

31 JOINS in Relation 1ABC100 2DEF75 3GHI36 4JKL2 5MNO0 P (part) P_no descrip qnty 27IBMNY 35MSFTWSH 8LSNJAX 14AMDCA 51AJRBNA 24UFGNV S (supplier) s_no name locat 2245.00 3356.50 2144.00 42410.00 1272.25 SP (Supplies) p_no s_no price

32 XML documents P.XML 1 ABC 100 S.XML 27 IBM NY SP.XML 2 24 5.00

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 { $s/name, $p/descrip } 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 Xml document TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in the Unix environment Stevens W. Addison-Wesley 65.95 The Economics of Technology and Content for Digital TV Gerbarg Darcy CITI Kluwer Academic Publishers 129.95

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

37 Expected Result TCP/IP Illustrated Advanced Programming in the Unix environment

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

39 Expected Results TCP/IP Illustrated Stevens W.

40 XQuery example 3 For each book found at both bn.com and amazon.com, list the title of the book and its price from each source: { 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 { $b/title } { $a/price/text() } { $b/price/text() } }

41 XQuery Example 4 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. { for $b in document("www.bn.com/bib.xml")//book where count($b/author) > 0 return { $b/title } { for $a in $b/author[position()<=2] return $a } { if (count($b/author) > 2) then else () } }

42 Result Data on the Web Abiteboul Serge Buneman Peter

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

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......


Download ppt "XQuery language Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti."

Similar presentations


Ads by Google