Download presentation
Presentation is loading. Please wait.
1
Querying XML and Semistructured Data
CSE 350 Fall 2003
2
Query 1 SELECT row: X FROM biblio.X WHERE “Smith” in X.author . . .
book paper book Answer = {row: {author:“Smith”, date: 1999, title: “Database…”}, } &o12 &o24 &o29 . . . author title date author title author date &25 &30 &o52 1976 Roux &96 Database Systems &o47 &o48 &o50 Combalusier Smith 1999 Database Systems
3
Query 2 SELECT author: X FROM biblio.book.author X . . .
Answer = {author: “Smith”, author: “Roux”, author: “Comalusier”} &o1 book paper book &o12 &o24 &o29 . . . author title date author title author date &25 &30 &o52 1976 Roux &96 Database Systems &o47 &o48 &o50 Combalusier Smith 1999 Database Systems
4
Query 3 SELECT row: ( SELECT author: X FROM X.author Y) FROM biblio.book X biblio &o1 book paper book Answer = {row: {author:“Smith”}, row: {author:“Roux”, author:“Combalusier”,}, } &o12 &o24 &o29 . . . author title date author title author date &25 &30 &o52 1976 Roux &96 Database Systems &o47 &o48 &o50 Combalusier Smith 1999 Database Systems
5
SELECT ( SELECT row: {author: Y, title: T} FROM X. author Y, X
SELECT ( SELECT row: {author: Y, title: T} FROM X.author Y, X.title T) FROM biblio.book X WHERE “Roux” in X.author Query 4 biblio &o1 book Answer = {row: {author:“Roux”, title: “Database…”}, row: {author:“Combalusier”, title: “Database…”}, } paper book &o12 &o24 &o29 . . . author title date author title author date &25 &30 &o52 1976 Roux &96 Database Systems &o47 &o48 &o50 Combalusier Smith 1999 Database Systems
6
Semantics Given query Q and database DB
SELECT E[X1, …, Xn] FROM F WHERE C Given query Q and database DB Answer(Q,DB) is defined in two steps Step 1: compute all bindings: Cij are node oids or atomic values Must satisfy paths in F and conditions in C Step 2: answer is E[C11, …, C1n] U … U E[Cm1, …, Cmn] For nested subqueries, apply semantics recursively X1 X2 … Xn Ci1 Ci2 Cin
7
XQuery http://www.w3.org/TR/xquery (12/2001) XML Query data model
Ordered FLWR (“Flower”) Expressions FOR ... LET... WHERE... RETURN...
8
XQuery Find all book titles published after 1995:
FOR $x IN document("bib.xml")/bib/book WHERE $x/year > 1995 RETURN $x/title Result: <title> abc </title> <title> def </title> <title> ghi </title>
9
XQuery For each author of a book by Morgan Kaufmann, list all books s/he published: FOR $a IN distinct(document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author) RETURN <result> $a, FOR $t IN /bib/book[author=$a]/title RETURN $t </result> distinct = a function that eliminates duplicates
10
XQuery Result: <result> <author>Jones</author>
<title> abc </title> <title> def </title> </result> <author> Smith </author> <title> ghi </title>
11
XQuery Summary of the FLWR structure
FOR: Binds node variables iteration LET: Binds collection variables one value FOR/LET Clauses WHERE Clause RETURN Clause List of tuples Instance of Xquery data model
12
XQuery FOR $x in expr -- binds $x to each value in the list expr
LET $x = expr -- binds $x to the entire list expr Useful for common subexpressions and for aggregations
13
FOR v.s. LET FOR $x IN document("bib.xml")/bib/book
Returns: <result> <book>...</book></result> ... FOR $x IN document("bib.xml")/bib/book RETURN <result> $x </result> LET $x IN document("bib.xml")/bib/book RETURN <result> $x </result> Returns: <result> <book>...</book> <book>...</book> ... </result>
14
XQuery Example: Find books whose price is larger than avg.
Example: using distinct and count LET $a=avg(document("bib.xml")/bib/book/price) FOR $b in document("bib.xml")/bib/book WHERE $b/price > $a RETURN $b <big_publishers> FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN $p </big_publishers>
15
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...) $b/price list of n prices $b/price * list of n numbers RETURN <result> $b/author </result> Returns: <result> <author>...</author> <author>...</author> ... </result>
16
Sorting in XQuery <publisher_list> FOR $p IN distinct(document("bib.xml")//publisher) RETURN <publisher> <name> $p/text() </name> , FOR $b IN document("bib.xml")//book[publisher = $p] RETURN <book> $b/title , $b/price </book> SORTBY(price DESCENDING) </publisher> SORTBY(name) </publisher_list> Sorting arguments: refer to the name space of the RETURN clause, not the FOR clause
17
More Xquery: If-Then-Else
FOR $h IN //holding RETURN <holding> $h/title, IF = "Journal" THEN $h/editor ELSE $h/author </holding> SORTBY (title) More Xquery: existential & universal quantifiers FOR $b IN //book WHERE SOME $p IN $b//para SATISFIES contains($p, "sailing") AND contains($p, "windsurfing") RETURN $b/title " FOR $b IN //book WHERE EVERY $p IN $b//para SATISFIES contains($p, "sailing") RETURN $b/title
18
Other Stuff in XQuery BEFORE and AFTER FILTER
for dealing with order in the input FILTER deletes some edges in the result tree No GROUPBY currently in Xquery Some recent proposals exist Recursive functions Currently: arbitrary recursion Perhaps more restrictions in the future ?
19
Lorel Minor syntactic differences in regular path expressions (% instead of _, # instead of _*) Existential variables: What happens with books having multiple authors ? Author is existentially quantified: SELECT biblio.book.year FROM biblio.book WHERE biblio.book.author = “Roux” SELECT biblio.book.year FROM biblio.book X, X.author Y WHERE Y = “Roux”
20
UnQL Patterns: Equivalent to:
SELECT row: X WHERE {biblio.book: {author “Roux”, title X}} in DB, SELECT row: X FROM biblio.book Y, Y.author Z, Y.title X WHERE Z=“Roux”
21
UnQL Label variables: “find all publication types and their titles where Roux is an author” SELECT row: {type: L, title : Y} WHERE {biblio.L: {author “Roux”, title X}} in DB,
22
UnQL Unrestricted use of label variables creates problems: So, in UnQL regular path expressions cannot contain label variables: Pat ::= Var | Const | {L1:Pat1, …, Ln:Patn} L ::= RegularPathExpression | LabelVariable SELECT row: {type: L, title : Y} WHERE {biblio.(book|L).title X} in DB, SELECT row: {type: L, title : Y} WHERE {biblio.(L)*.title X} in DB,
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.