Query Form

Available local documents: bib.xml and phd.xml.


XQuery #1

(:
  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.
:)

let $d := doc('bib.xml')
for $a in distinct-values($d//author)
return
   <author name="{$a}">
   {
    for $b in $d//book[author=$a]
    return $b/title
   }
   </author>

XQuery #2

(:
  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.
:)

let $d := doc('bib.xml')
for $b in $d//book
return
   <book title="{data($b/title)}">
   {
    for $a in $b/author[position() <= 2]
    return $a
   }
   {
    if (count($b/author) > 2)
    then <et-al/>
    else ()
   }
   </book>

XQuery #3

(:
  Find all books such that:

   1. the name of some element ends with the string "or" and the
     same element contains the string "Dan" somewhere in its
     content, and
   2. some element contains the string "Morgan" somewhere in its
     content.

  For each such book, return the title and the
  qualifying elements.
:)

let $d := doc('bib.xml')
for $b in $d//book
let $e1 := $b/*[contains(string(.), "Dan")
           and ends-with(local-name(.), "or")]
let $e2 := $b/*[contains(string(.), "Morgan")]
where exists($e1) and exists($e2)
return
   <book>
   { $b/title }
   { $e1 }
   { $e2 }
   </book>

XQuery #4

(:
  For each author, show his/her full name as a single string and the
  number of books he/she has authored.
:)

let $d := doc('bib.xml')
for $da in distinct-values($d//author)
let $group := $d//author[. = $da]
return
   <author
     name="{concat($group[1]/first/text(), ' ', $group[1]/last/text())}"
     published="{count($d//book[author = $group[1]])}" />

XQuery #5

(:
  Find pairs of books that have different titles but the same set of
  authors (possibly in a different order).
:)

let $d := doc('bib.xml')
for $book1 in $d//book, $book2 in $d//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 deep-equal($aut1, $aut2)
return
   <book-pair>
   { $book1/title }
   { $book2/title }
   </book-pair>

XQuery #6

(:
  For each book with an author, return the book with its title and
  authors. For each book with an editor, return reference with the
  book title and the editor's affiliation.
:)

let $d := doc('bib.xml')
for $b in $d//book
return
   if ($b[author]) then
   <book>
   { $b/title }
   { $b/author }
   </book>
   else if ($b[editor]) then
   <reference>
   { $b/title }
   { $b/editor/affiliation }
   </reference>
   else ()

XQuery #7

(:
  Return the first and last name of all people who are authors
  (or co-authors) of all the books.
:)

let $d := doc('bib.xml')
for $da in distinct-values($d//author)
let $group := $d//author[. = $da]
where
     every $b in $d//book[author] satisfies
     some $a2 in $b/author satisfies ($a2 = $group[1])
return $group[1]

XQuery #8

(:
  Display the book information using year elements and, for
  each year, display the title of the book as an attribute.
:)

let $d := doc('bib.xml')
for $a in $d//*[@year]
return <year book="{$a/title}"> {string($a/@year)} </year>

XQuery #9

(:
  Display the total number of authors, both non-distinct and distinct.
:)

let $d := doc('bib.xml')
return
   <authors
     total="{count($d//author)}"
     distinct="{count(distinct-values($d//author))}" />

XQuery #10: Your turn!

(:
  Find all Witold Lipski's PhD students.
:)

XQuery #11: Your turn!

(:
  Find all Alfred Tarski's descendants who graduated from a US university.
:)

XQuery #12: Your turn!

(:
  Who are the common ancestors of Krzysztof Apt and Jan Chomicki?
:)

XQuery #13: Your turn!

(:
  For every PhD recipient, compute the number of descendants.
:)

Valid W3C XHTML 1.0 StrictValid W3C CSS 2.1