List books published by Addison-Wesley after 1991, including their year and title.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
where $b/publisher = "Addison-Wesley" and $b/@year > 1991
return
{ $b/title }
}
Expected Result:
TCP/IP Illustrated
Advanced Programming in the Unix environment
Create a flat list of all the title-author pairs, with each pair enclosed in a "result" element.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book,
$t in $b/title,
$a in $b/author
return
{ $t }
{ $a }
}
Expected Result:
TCP/IP Illustrated
Stevens
W.
Advanced Programming in the Unix environment
Stevens
W.
Data on the Web
Abiteboul
Serge
Data on the Web
Buneman
Peter
Data on the Web
Suciu
Dan
For each book in the bibliography, list the title and authors, grouped inside a "result" element.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
return
{ $b/title }
{ $b/author }
}
Expected Result:
TCP/IP Illustrated
Stevens
W.
Advanced Programming in the Unix environment
Stevens
W.
Data on the Web
Abiteboul
Serge
Buneman
Peter
Suciu
Dan
The Economics of Technology and Content for Digital TV
For each author in the bibliography, list the author's name and the titles of all books by that author, grouped inside a "result" element.
Solution in XQuery:
{
let $a := doc("http://bstore1.example.com/bib/bib.xml")//author
for $last in distinct-values($a/last),
$first in distinct-values($a[last=$last]/first)
order by $last, $first
return
{ $last }
{ $first }
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
where some $ba in $b/author
satisfies ($ba/last = $last and $ba/first=$first)
return $b/title
}
}
The order in which values are returned by distinct-values() is undefined. The distinct-values() function returns atomic values, extracting the names from the elements.
Expected Result:
Abiteboul
Serge
Data on the Web
Buneman
Peter
Data on the Web
Stevens
W.
TCP/IP Illustrated
Advanced Programming in the Unix environment
Suciu
Dan
Data on the Web
For each book (in reviews xml file) found at both bstore1.example.com and bstore2.example.com, list the title of the book and its price from each source.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")//book,
$a in doc("http://bstore2.example.com/reviews.xml")//entry
where $b/title = $a/title
return
{ $b/title }
{ $a/price/text() }
{ $b/price/text() }
}
Expected Result:
TCP/IP Illustrated
65.95
65.95
Advanced Programming in the Unix environment
65.95
65.95
Data on the Web
34.95
39.95
For each book that has at least one author, list the title and first two authors, and an empty "et-al" element if the book has additional authors.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")//book
where count($b/author) > 0
return
{ $b/title }
{
for $a in $b/author[position()<=2]
return $a
}
{
if (count($b/author) > 2)
then
else ()
}
}
Expected Result:
TCP/IP Illustrated
Stevens
W.
Advanced Programming in the Unix environment
Stevens
W.
Data on the Web
Abiteboul
Serge
Buneman
Peter
List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")//book
where $b/publisher = "Addison-Wesley" and $b/@year > 1991
order by $b/title
return
{ $b/@year }
{ $b/title }
}
Expected Result:
Advanced Programming in the Unix environment
TCP/IP Illustrated
Find books in which the name of some element ends with the string "or" and the same element contains the string "Suciu" somewhere in its content. For each such book, return the title and the qualifying element.
Solution in XQuery:
for $b in doc("http://bstore1.example.com/bib.xml")//book
let $e := $b/*[contains(string(.), "Suciu")
and ends-with(local-name(.), "or")]
where exists($e)
return
{ $b/title }
{ $e }
In the above solution, string(), local-name() and ends-with() are functions defined in the Functions and Operators document.
Expected Result:
Data on the Web
Suciu
Dan
In the document "books.xml", find all section or chapter titles that contain the word "XML", regardless of the level of nesting.
Solution in XQuery:
{
for $t in doc("books.xml")//(chapter | section)/title
where contains($t/text(), "XML")
return $t
}
Expected Result:
XML
XML and Semistructured Data
In the document "prices.xml", find the minimum price for each book, in the form of a "minprice" element with the book title as its title attribute.
Solution in XQuery:
{
let $doc := doc("prices.xml")
for $t in distinct-values($doc//book/title)
let $p := $doc//book[title = $t]/price
return
{ min($p) }
}
Expected Result:
65.95
65.95
34.95
For each book with an author, return the book with its title and authors. For each book with an editor, return a reference with the book title and the editor's affiliation.
Solution in XQuery:
{
for $b in doc("http://bstore1.example.com/bib.xml")//book[author]
return
{ $b/title }
{ $b/author }
}
{
for $b in doc("http://bstore1.example.com/bib.xml")//book[editor]
return
{ $b/title }
{$b/editor/affiliation}
}
Expected Result:
TCP/IP Illustrated
Stevens
W.
Advanced Programming in the Unix environment
Stevens
W.
Data on the Web
Abiteboul
Serge
Buneman
Peter
Suciu
Dan
The Economics of Technology and Content for Digital TV
CITI
Find pairs of books that have different titles but the same set of authors (possibly in a different order).
Solution in XQuery:
{
for $book1 in doc("http://bstore1.example.com/bib.xml")//book,
$book2 in doc("http://bstore1.example.com/bib.xml")//book
let $aut1 := for $a in $book1/author
order by $a/last, $a/first
return $a
let $aut2 := for $a in $book2/author
order by $a/last, $a/first
return $a
where $book1 << $book2
and not($book1/title = $book2/title)
and deep-equal($aut1, $aut2)
return
{ $book1/title }
{ $book2/title }
}
Expected Result:
TCP/IP Illustrated
Advanced Programming in the Unix environment