Docsity

XQuery: Advanced Database Systems Lecture Notes, Slides of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
XQuery
CPS 216
Advanced Database Systems
2
Announcements (March 21)
Midterm has been graded
Homework #3 will be assigned next Tuesday
Reading assignment due next Wednesday
XML processing in Lore (VLDB 1999) and Niagara
(VLDB 2003)
Project milestone 2 due next Thursday
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 in general can return a new
result XML document
Compare with an XPath expression, which always
returns a sequence of nodes from the input document or
atomic values (boolean, number, string, etc.)
pf3
pf4
pf5

Partial preview of the text

Download XQuery: Advanced Database Systems Lecture Notes and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

XQuery

CPS 216

Advanced Database Systems

2

Announcements (March 21)

™ Midterm has been graded

™ Homework #3 will be assigned next Tuesday

™ Reading assignment due next Wednesday

ƒ XML processing in Lore ( VLDB 1999) and Niagara

( VLDB 2003)

™ Project milestone 2 due next Thursday

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 in general can return a new

result XML document

ƒ Compare with an XPath expression, which always

returns a sequence of nodes from the input document or

atomic values (boolean, number, string, etc.)

A simple XQuery based on XPath

Find all books with price lower than $

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

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

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

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

ƒ The XPath expression returns a sequence 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 doc(“bib.xml”)/bibliography/book let $p := $b/publisher where $b/year < 2000 return { $b/title } { $p } }

™ for: loop ƒ $b ranges over the result sequence, getting one item 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 doc(“bib.xml”)/bibliography/book[year<2000] return { $b/title } { $b/publisher } }

An explicit join

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

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

11

Existentially quantified expressions

(some $ var in collection 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 doc(“bib.xml”)//book where (some $section in $b//section satisfies contains(string($section), “XML”)) return $b/title }

12

Universally quantified expressions

(every $ var in collection 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 doc(“bib.xml”)//book where (every $section in $b//section satisfies contains(string($section), “XML”)) return $b/title }

Aggregation

™ List each publisher and the average prices of all its books

{ for $pub in distinct-values(doc(“bib.xml”)//publisher) let $price := avg(doc(“bib.xml”)//book[publisher=$pub]/@price) return {$pub} {$price} }

ƒ distinct-values( collection ) removes duplicates by value

  • If the collection consists of elements (with no explicitly declared types), they are first converted to strings representing their text contents

ƒ avg( collection ) computes the average of collection (assuming each

item in collection can be converted to a numeric value)

14

Sorting (a brief history)

™ XPath always returns a sequence of nodes in original

document order

™ for loop will respect the ordering in the sequence

™ August 2002

ƒ Introduce an operator sort by ( sort-by-expression-list ) to output

results in a user-specified order

ƒ Example: list all books with price higher than $100, in order by

first author; for books with the same first author, order by title

{

doc(“bib.xml”)//book[@price>100]

sort by (author[1], title)

}

15

Tricky semantics

™ List titles of all books, sorted by their prices

{ (doc(“bib.xml”)//book sort by (@price))/title }

ƒ What is wrong?