IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy
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
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
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.
FOR v.s. LET FOR Binds node variables iteration LET Binds collection variables one value
Bib.xml TCP/IP Illustrated Stevens W. Addison-Wesley Advanced Programming in the Unix environment Stevens W. Addison-Wesley Data on the Web Abiteboul Serge Buneman Peter Suciu Dan Morgan Kaufmann Publishers The Economics of Technology and Content for Digital TV Gerbarg Darcy CITI Kluwer Academic Publishers
Bib.dtd
Books.xml Data Model Syntax For Data Model XML Basic Syntax XML and Semistructured Data
Books.xml
Reviews.xml Data on the Web A very good discussion of semi-structured database systems and XML. Advanced Programming in the Unix environment A clear and detailed discussion of UNIX programming. TCP/IP Illustrated One of the best books on TCP/IP.
Reviews.dtd
Prices.xml Advanced Programming in the Unix environment bstore2.example.com Advanced Programming in the Unix environment bstore1.example.com TCP/IP Illustrated bstore2.example.com TCP/IP Illustrated bstore1.example.com Data on the Web bstore2.example.com Data on the Web bstore1.example.com 39.95
Prices.dtd
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 > 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
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
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
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} }
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
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> <book-with- prices> Advanced Programming in the Unix environment Data on the Web
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 ()} }
Query 6 TCP/IP Illustrated Stevens W. Advanced Programming in the Unix environment Stevens W. Data on the Web Abiteboul Serge Buneman Peter
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 > 1991 order by $b/title return { $b/title } } Advanced Programming in the Unix environment TCP/IP Illustrated
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
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
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) } }
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} }
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
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 } }
Query 12 TCP/IP Illustrated Advanced Programming in the Unix environment
Thanks