Query Languages For XML: Xpath Xquery XSLT (Not Being Covered Today!)
Query Languages For XML: Xpath Xquery XSLT (Not Being Covered Today!)
XPath
XQuery
XSLT (not being covered today!)
QSX (LN 3)
Joins, aggregation
Transform XML values from one schema to another
XML construction
QSX (LN 3)
Query Languages
XPath
QSX (LN 3)
QSX (LN 3)
DTD
<!ELEMENT
bib
(book*) >
<!ELEMENT
book
<!ATTLIST
book
year
CDATA
<!ELEMENT
<!ELEMENT
editor
<!ELEMENT
#required >
QSX (LN 3)
Data model
Node-labeled, ordered tree
bib
book
book
last
first
last
first
last
QSX (LN 3)
first
XPath
W3C standard: www.w3.org/TR/xpath
Navigating an XML tree and finding parts of the tree (node
QSX (LN 3)
XPath constructs
XPath query Q:
Tree traversal: downward, upward, sideways
Relational/Boolean expressions: qualifiers (predicates)
Functions: aggregation (e.g., count), string functions
//author[last=Bush]
//book[author/last=Bush]/title | //book[author/last=Blair]/title
bib
book
book
last
first
last
first
QSX (LN 3)
first
Downward traversal
Syntax:
Q ::= . |
q ::= Q |
| @l
Q op c
| Q/Q
Q|Q
| q and q
| //Q
q or q
| /Q
Q[q]
| not(q)
Examples:
parent/child: /bib/book
ancestor//descendant: bib//last, //last
wild card:
bib/book/*
attributes:
bib/book/@year
book
last
first
last
first
first
10
Filters (qualifiers)
//book[price]/title
1991
//book[title and author and not(price)]/title
/[//[not(@id)]]?
/[not(//[not(@id))]] ?
QSX (LN 3)
11
Upward traversal
Syntax:
Q ::=
...
| ../Q
| ancestor ::Q
ancestor-or-self::Q
../: parent
ancestor, ancestor-or-self: recursion
Example:
//author[../title = WMD]/last
find the last names of authors of books with the title WMD
ancestor :: book[//last=Bush]
./author
QSX (LN 3)
12
Sideways
Syntax:
Q ::=
...
following-sibling ::Q
preceding-sibling::Q
Example:
following-sibling :: book [//last=Bush]
find the books that are right siblings and are written by Bush
preceding-sibling :: book[//last=Bush]
find the books that are left siblings and are written by Bush
QSX (LN 3)
13
XPath
XQuery
XSLT
QSX (LN 3)
14
XQuery
W3C working draft www.w3.org/TR/xquery
Functional, strongly typed query language: Turing-complete
XQuery = XPath +
http://www-db.research.bell-labs.com/galax/
QSX (LN 3)
http://www.saxonica.com
15
FLWR Expressions
For, Let, Where, OrderBy, return
Q1: Find titles and authors of all books published by AddisonWesley after 1991.
<answer>{
for $book in /bib/book
where $book/@year > 1991 and $book/publisher=Addison-Wesley
return
<book>
<title> {$book/title } </title>,
for $author in $book/author return
<author> {$author } </author>
</book>
}</answer>
for loop; $x: variable
where: condition test; selection
QSX
3)its value
return: evaluate an expression
and(LN
return
16
join
Find books that cost more at Amazon than at BN
<answer>{
let $amazon := doc(http://www.amozon.com/books.xml),
$bn := doc(http://www.BN.com/books.xml)
for
$a in $amozon/books/book,
$b in $bn/books/book
where
$a/isbn = $b/isbn
and
QSX (LN 3)
17
Conditional expression
Q2: Find all book titles, and prices where available
<answer>{
for $book in /bib/book
return <book>
<title> {$book/title } </title>,
{ if $book[price]
then <price> {$book/price } </price>
else ( ) }
</book>
}</answer>
QSX (LN 3)
18
QSX (LN 3)
19