Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy.

Similar presentations


Presentation on theme: "IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy."— Presentation transcript:

1 IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy

2 Introduction What is XQuery? - XQuery is to XML what SQL is to database tables. - XQuery was designed to query XML data. - XQuery is built on XPath expressions - XQuery is supported by all major databases - XQuery is a W3C Recommendation

3 Introduction XQuery can be used to: - Extract information to use in a Web Service - Generate summary reports - Transform XML data to XHTML - Search Web documents for relevant information

4 FLWR (“Flower”) Expressions for $x in doc("bib.xml")/bib/book where $x/price>30 order by $x/title return $x/title FLWOR is an acronym for "For, Let, Where, Order by, Return". The for clause selects all book elements under the bib element into a variable called $x. The where clause selects only book elements with a price element with a value greater than 30. The order by clause defines the sort-order. Will be sort by the title element. The return clause specifies what should be returned. Here it returns the title elements.

5 FOR v.s. LET FOR Binds node variables  iteration LET Binds collection variables  one value

6 Bib.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in the Unix environment Stevens W. Addison-Wesley 65.95 Data on the Web Abiteboul Serge Buneman Peter Suciu Dan Morgan Kaufmann Publishers 39.95 The Economics of Technology and Content for Digital TV Gerbarg Darcy CITI Kluwer Academic Publishers 129.95

7 Bib.dtd

8 Books.xml Data Model Syntax For Data Model XML Basic Syntax XML and Semistructured Data

9 Books.xml

10 Reviews.xml Data on the Web 34.95 A very good discussion of semi-structured database systems and XML. Advanced Programming in the Unix environment 65.95 A clear and detailed discussion of UNIX programming. TCP/IP Illustrated 65.95 One of the best books on TCP/IP.

11 Reviews.dtd

12 Prices.xml Advanced Programming in the Unix environment bstore2.example.com 65.95 Advanced Programming in the Unix environment bstore1.example.com 65.95 TCP/IP Illustrated bstore2.example.com 65.95 TCP/IP Illustrated bstore1.example.com 65.95 Data on the Web bstore2.example.com 34.95 Data on the Web bstore1.example.com 39.95

13 Prices.dtd

14 Query 1 List books published by Addison-Wesley after 1991, including their year and title { for $b in doc("bib.xml")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return { $b/title } } The doc() function is used to open the “bib.xml" TCP/IP Illustrated <book year="1992"> Advanced Programming in the Unix environment

15 Query 2 Create a flat list of all the title-author pairs, with each pair enclosed in a "result" element. {for $b in doc("bib.xml")/bib/book, $t in $b/title, $a in $b/author return { $t } { $a } } TCP/IP Illustrated Stevens W. Advanced Programming in the Unix environment Stevens W. Data on the Web Abiteboul Serge Data on the Web Buneman Peter Data on the Web Suciu Dan

16 Query 3 For each book in the bibliography, list the title and authors, grouped inside a "result" element. { for $b in doc("bib.xml")/bib/book return { $b/title } { $b/author } } TCP/IP Illustrated Stevens W. Advanced Programming in the Unix environment Stevens W. Data on the Web Abiteboul Serge Buneman Peter Suciu Dan The Economics of Technology and Content for Digital TV

17 Query 4 For each author in the bibliography, list the author's name and the titles of all books by that author, grouped inside a "result" {let $a := doc("bib.xml")//author for $last in distinct-values($a/last), $first in distinct-values($a[last=$last]/first) order by $last, $first return { $last } { $first } {for $b in doc("bib.xml")/bib/book where some $ba in $b/author satisfies ($ba/last = $last and $ba/first=$first) return $b/title} }

18 Query 4 Abiteboul Serge Data on the Web Buneman Peter Data on the Web Stevens W. TCP/IP Illustrated Advanced Programming in the Unix environment Suciu Dan Data on the Web

19 Query 5 For each book found in bib.xml and reviews.xml, list the title of the book and its price from each source { for $b in doc("bib.xml")//book, $a in doc("reviews.xml")//entry where $b/title = $a/title return { $b/title } { $a/price/text() } { $b/price/text() } } TCP/IP Illustrated 65.95</price- reviews-source> 65.95 <book-with- prices> Advanced Programming in the Unix environment 65.95 65.95 Data on the Web 34.95 39.95

20 Query 6 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 doc("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 ()} }

21 Query 6 TCP/IP Illustrated Stevens W. Advanced Programming in the Unix environment Stevens W. Data on the Web Abiteboul Serge Buneman Peter

22 Query 7 List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order. { for $b in doc("bib.xml")//book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 order by $b/title return { $b/title } } Advanced Programming in the Unix environment TCP/IP Illustrated

23 Query 8 Find books in which the name of some element ends with the string "or" and the same element contains the string "Suciu" somewhere in its content. For each such book, return the title and the qualifying element. for $b in doc("bib.xml")//book let $e := $b/*[contains(string(.), "Suciu") and ends-with(local-name(.), "or")] where exists($e) return { $b/title } { $e } Data on the Web Suciu Dan

24 Query 9 In the document "books.xml", find all section or Section titles that contain the word "XML", regardless of the level of nesting { for $t in doc("books.xml")//(Section | section)/title where contains($t/text(), "XML") return $t } XML XML and Semistructured Data

25 Query 10 In the document "prices.xml", find the minimum price for each book, in the form of a "minprice" element with the book title as its title attribute. { let $doc := doc("prices.xml") for $t in distinct-values($doc//book/title) let $p := $doc//book[title = $t]/price return { min($p) } } 65.95 34.95

26 Query 11 For each book with an author, return the book with its title and authors. For each book with an editor, return a reference with the book title and the editor's affiliation. {for $b in doc("bib.xml")//book[author] return {$b/title } {$b/author } } {for $b in doc("bib.xml")//book[editor] return {$b/title } {$b/editor/affiliation} }

27 Query 11 TCP/IP Illustrated Stevens W. Advanced Programming in the Unix environment Stevens W. Data on the Web Abiteboul Serge Buneman Peter Suciu Dan The Economics of Technology and Content for Digital TV CITI

28 Query 12 Find pairs of books that have different titles but the same set of authors (possibly in a different order) {for $book1 in doc("bib.xml")//book, $book2 in doc("bib.xml")//book let $aut1 := for $a in $book1/author order by $a/last, $a/first return $a let $aut2 := for $a in $book2/author order by $a/last, $a/first return $a where $book1 << $book2 and not($book1/title = $book2/title) and deep-equal($aut1, $aut2) return { $book1/title } { $book2/title } }

29 Query 12 TCP/IP Illustrated Advanced Programming in the Unix environment

30 Thanks


Download ppt "IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy."

Similar presentations


Ads by Google