Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS432: Semi-Structured Data Dr. Azeddine Chikh. 7. XQuery.

Similar presentations


Presentation on theme: "IS432: Semi-Structured Data Dr. Azeddine Chikh. 7. XQuery."— Presentation transcript:

1 IS432: Semi-Structured Data Dr. Azeddine Chikh

2 7. XQuery

3 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

4 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

5 FLWR (“Flower”) Expressions 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 $x in doc("bib.xml")/bib/book where $x/price>30 order by $x/title return $x/title

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

7 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

8 Bib.dtd

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

10 Books.xml

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

12 Reviews.dtd

13 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

14 Prices.dtd

15 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 } } { for $b in doc("bib.xml")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return { $b/title } } Query 1 TCP/IP Illustrated Advanced Programming in the Unix environment The doc() function is used to open the “bib.xml"

16 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 } } {for $b in doc("bib.xml")/bib/book, $t in $b/title, $a in $b/author return { $t } { $a } } Query 2 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

17 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 } } { for $b in doc("bib.xml")/bib/book return { $b/title } { $b/author } } Query 3 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

18 For each author in the bibliography, list the author's name and the titles of all books by that author, grouped inside a "result" element {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} } {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

19 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

20 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() } } { 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() } } Query 5 TCP/IP Illustrated 65.95 65.95 Advanced Programming in the Unix environment 65.95 65.95 Data on the Web 34.95 39.95

21 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 ()} } {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

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

23 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 } } { for $b in doc("bib.xml")//book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 order by $b/title return { $b/title } } Query 7 Advanced Programming in the Unix environment TCP/IP Illustrated

24 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 } 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 } Query 8 Data on the Web Suciu Dan

25 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 } { for $t in doc("books.xml")//(Section | section)/title where contains($t/text(), "XML") return $t } Query 9 XML XML and Semistructured Data

26 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) } } { let $doc := doc("prices.xml") for $t in distinct-values($doc//book/title) let $p := $doc//book[title = $t]/price return { min($p) } } Query 10 65.95 34.95

27 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} } {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

28 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

29 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 } } {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

30 TCP/IP Illustrated Advanced Programming in the Unix environment


Download ppt "IS432: Semi-Structured Data Dr. Azeddine Chikh. 7. XQuery."

Similar presentations


Ads by Google