XQuery: A Powerful Query Language for XML Data, Slides of Introduction to Database Management Systems

An introduction to xquery, a language for querying and transforming xml data. Xquery combines the features of xpath and a full-fledged sql-like query language. It allows users to write expressions using xpath, flwr expressions, quantified expressions, and aggregation functions. The document also covers various examples of xquery expressions and their results.

Typology: Slides

2011/2012

Uploaded on 01/29/2012

arold
arold 🇺🇸

4.7

(24)

372 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
XQuery
CPS 116
Introduction to Database Systems
2
Announcements
Homework #3 will be assigned this Thursday
(October 21)
3
XQuery
XPath + full-fledged SQL-like query language
XQuery expressions can be
XPath expressions
FLWR (C) expressions
Quantified expressions
Aggregation, sorting, and more…
An XQuery expression returns a result XML
documents
Compare with an XPath expression, which returns a
node-set or an atomic value (boolean, number, string)
4
A simple XQuery based on XPath
Find all books with price lower than $50
<result>
{
document(“bib.xml”)/bibliography/book[@price<50]
}
</result>
Things outside {}’s are copied to output verbatim
Things inside {}’s are evaluated and replaced by the results
document(“bib.xml”) specifies the document to query
The XPath expression returns a set of book elements
These elements (including all their descendents) are copied to
output
5
FLWR expressions
Retrieve the titles of books published before 2000,
together with their publisher
<result>{
for $b in document(“bib.xml”)/bibliography/b ook
let $p := $b/publisher
where $b/year < 2000
return
<book>
{ $b/title }
{ $p }
</book>
}</result>
for: loop
$b ranges over the result node-set,
getting one node at a time
let: assignment
$p gets the entire result of
$b/publisher (possibly many nodes)
where: filter condition
return: result structuring
Invoked in the “innermost loop,” i.e.,
once for each successful binding of all
query variables
6
An equivalent formulation
Retrieve the titles of books published before 2000,
together with their publisher
<result>{
for $b in
return
<book>
{ $b/title }
{ $b/publisher }
</book>
}</result>
document(“bib.xml”)/bibliography/book[year<2 000]
pf3

Partial preview of the text

Download XQuery: A Powerful Query Language for XML Data and more Slides Introduction to Database Management Systems in PDF only on Docsity!

XQuery

CPS 116

Introduction to Database Systems

Announcements

™ Homework #3 will be assigned this Thursday

(October 21)

3

XQuery

™ XPath + full-fledged SQL-like query language

™ XQuery expressions can be

ƒ XPath expressions

ƒ FLWR (C) expressions

ƒ Quantified expressions

ƒ Aggregation, sorting, and more…

™ An XQuery expression returns a result XML

documents

ƒ Compare with an XPath expression, which returns a

node-set or an atomic value (boolean, number, string)

4

A simple XQuery based on XPath

Find all books with price lower than $

document(“bib.xml”)/bibliography/book[@price<50]

™ Things outside {}’s are copied to output verbatim

™ Things inside {}’s are evaluated and replaced by the results

ƒ document(“bib.xml”) specifies the document to query

ƒ The XPath expression returns a set of book elements

ƒ These elements (including all their descendents) are copied to

output

5

FLWR expressions

™ Retrieve the titles of books published before 2000,

together with their publisher

{ for $b in document(“bib.xml”)/bibliography/book let $p := $b/publisher where $b/year < 2000 return { $b/title } { $p } }

™ for: loop ƒ $b ranges over the result node-set, getting one node at a time ™ let: assignment ƒ $p gets the entire result of $b/publisher (possibly many nodes) ™ where: filter condition ™ return: result structuring ƒ Invoked in the “innermost loop,” i.e., once for each successful binding of all query variables

6

An equivalent formulation

™ Retrieve the titles of books published before 2000,

together with their publisher

{ for $b in return { $b/title } { $b/publisher } }

document(“bib.xml”)/bibliography/book[year<2000]

Another formulation

™ Retrieve the titles of books published before 2000,

together with their publisher

{ for $b in document(“bib.xml”)/bibliography/book, $p in $b/publisher where $b/year < 2000 return { $b/title } { $p } }

™ Is this query equivalent to the previous two?

™ Yes, if there is one publisher per book

™ No, in general

ƒ Two result book elements will be created for a book with two publishers ƒ No result book element will be created a book with no publishers

Yet another formulation

™ Retrieve the titles of books published before 2000,

together with their publisher

{ let $b := document(“bib.xml”)/bibliography/book where $b/year < 2000 return { $b/title } { $b/publisher } }

™ Is this query correct?

™ No!

™ It will produce only one output book

element, with all titles clumped together

and all publishers clumped together

™ All books will be processed (as long as one is

published before 2000

9

Subqueries in return

™ Extract book titles and their authors; make title an

attribute and rename author to writer

{ for $b in document(“bib.xml”)/bibliography/book return { for $a in $b/author return {string($a)} } }

10

An explicit join

™ Find pairs of books that have common author(s)

{ for $b1 in document("bib.xml")//book for $b2 in document("bib.xml")//book where $b1/author = $b2/author return {$b1/title} {$b2/title} }

11

Existentially quantified expressions

(some $ var in node-set satisfies condition )

ƒ Can be used in where as a condition

™ Find titles of books in which XML is mentioned in

some section

{ for $b in document(“bib.xml”)//book where (some $section in $b//section satisfies contains(string($section), “XML”)) return $b/title }

12

Universally quantified expressions

(every $ var in node-set satisfies condition )

ƒ Can be used in where as a condition

™ Find titles of books in which XML is mentioned in

every section

{ for $b in document(“bib.xml”)//book where (every $section in $b//section satisfies contains(string($section), “XML”)) return $b/title }