Download XQuery and XML Querying: A University Course by Farnoush Banaei-Kashani - Spring 2008 and more Study notes Deductive Database Systems in PDF only on Docsity!
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Querying XML
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
XQuery References
http://www.w3.org/TR/xquery/XQuery 1.0: An XML Query Language
http://www.w3.org/TR/xmlquery-use-casesXML Query Use Cases
http://www.gnu.org/software/qexo/Qexo: The GNU Kawa implementation of XQuery
XQuery Tutorial by M
ø
ller & Schwartzbach
http://www.brics.dk/~amoeller/XML/querying/
tutorial/xquery-tutorial.pdfhttp://homepages.inf.ed.ac.uk/wadler/papers/xquery-Xquery Tutorial by Fankhauser & Wadler
http://www.galaxquery.org/Galax: an open-source XQuery implementation
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Why a new query language?
Semi-structured: XML data is not rigidly structured
Self-describing: schema exists with data
Can naturally model irregularities
Missing elements (e.g., bestseller?)
Multiple occurrences of the same element (reviews*)
values in othersElements w/ atomic values in some data items and structured
Collections of elements with heterogeneous structure
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
XQuery 1.
XPath, XQL, XML-QL, Lorel, YATL, SQL, and OQL.Based on XML query language Quilt, with borrowed features from
same expressions will generate the same results.XQuery 1.0 relies on XPath 2.0 and XML Schema datatypes. The
Basic definitions:
It is a functional language and strongly typed
Basic building block:
(^) expression
The value of an expression is always a
(^) sequence
: a collection of zero or
more
(^) items
An item is either an
(^) atomic value
(^) or a
(^) node
A node conforms to one of
(^) 7 node types:
(root) nodeElement, attribute, namespace, text, comment, processing-instruction, and document
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Example XML Document
A fine book.Data on the WebSuciuBunemanAbiteboul
The best ever!XML in ScotlandBuneman
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
XQuery Examples
Titles of all books published before 2000:
/BOOKS/BOOK[@YEAR < 2000]/TITLE
Year and title of all books published before 2000:
return { $book/@YEAR, $book/TITLE }where $book/@YEAR < 2000for $book in /BOOKS/BOOK
Books grouped by author:
for $author in distinct(/BOOKS/BOOK/AUTHOR) return { /BOOKS/BOOK[AUTHOR = $author]/TITLE
}
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Path Expressions
The simplest kind of query is just an
XPath 2.
doc("recipes.xml")//recipe[title="Ricotta Pie"]//ingredient[@amount] expression. A simple path expression looks like:
doc("recipes.xml") The initial context for the path expression is given by
// means any child node (not just the immediate child node)
recipe collectionThe result is all simple ingredients used to prepare Ricotta Pie in the
(^) (http://www.brics.dk/~amoeller/XML/xml/recipes.xml)
ingredient The result is given as a list of XML fragments, each rooted with an
(^) element
The
(^) order
(^) of the fragments respects the document order (order matters!
Only return ingredients with
amount
attribute
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
FLWOR Expressions
The
main engine
of XQuery is the FLWOR expression:
F
or-
L
et-
W
here-
O
rder-
R
eturn
Pronounced "flower"
returnorder by avg($e/salary) descendingwhere count($e) >= 10let $e := doc("emps.xml")//employee[deptno = $d] for $d in doc("depts.xml")//deptno example:Generalizes SELECT-FROM-HAVING-WHERE from SQL;
<big-dept> { $d, {avg($e/salary)}{count($e)},
</big-dept>
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
FLWOR Expressions
(cont
d)
Note the difference between
for
and
let
for $x in /company/employee
Generates a list of bindings of
$x
to each
employee
element in
the
company
, but:
let $x := /company/employee
Generates a single binding of
$x
to the list of
employee
elements
in the
company
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Projection
Return all authors of all books:
/BOOKS/BOOK/AUTHOR
BunemanSuciu,Buneman,Abiteboul,Returns:
The same query can also be written as a for loop:
for $dot1 in $root/BOOKS return $dot1/BOOK/AUTHOR
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Selection
(cont
d)
Return book with title
Data on the Web
/BOOKS/BOOK[TITLE = "Data on the Web"]
Returns: A fine book.Data on the WebSuciuBunemanAbiteboul
Return the review of the book with the title
Data on the
Web
/BOOKS/BOOK[TITLE = "Data on the Web"]/REVIEW
A fine book.Returns:
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Construction
Return year and title of all books published before 2000
returnwhere $book/@YEAR < 2000for $book in /BOOKS/BOOK { $book/@YEAR, $book/TITLE }
Data on the WebReturns: USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
Join
for $am in $amazon/BOOKS/BOOK,$fatbrain := doc("http://www.fatbrain.com/books.xml")let $amazon := doc("http://www.amazon.com/books.xml"),Fatbrain:Return the books that cost more at Amazon than at
$fat in $fatbrain/BOOKS/BOOK
where $am/ISBN = $fat/ISBN and $am/PRICE > $fat/PRICE
return { $am/TITLE, $am/PRICE, $fat/PRICE }
$p/income } return { $p/ssn } { $n/name } {for $n in doc("neighbors.xml")//neighbor[ssn = $p/ssn]for $p in doc("www.irs.gov/taxpayers.xml")//personReturn the name and income of your neighbors:
USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani
^ Other XML Query Languages
XML-QL
Lore (Lightweight Object Repository) & Lorel
XSL, easy to express recursive processing:
All
(^) author
elements, regardless of how deep they occur in the data:
<xsl:template>
<xsl:apply-templates/>
</xsl:template>
<xsl:template
match="author">
<xsl:value-of/>
</xsl:template>
constructing resultsXQL: XSL match patterns+some concise syntax for
with a GUIXML-GL: similar in expressiveness power to XML-QL but
WebL: markup algebra + service combinators