<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<!-- a bookstore database -->
<book isbn=“111111” cat=“fiction”>
<!-- a particular book -->
<title lang=“chn”>Harry Potter</title>
<price unit=“us”>79.99</price>
</book>
<book isbn=“222222” cat=“textbook”>
<title lang=“eng”>Learning XML</title>
<price unit=“us”>69.95</price>
</book>
<book isbn "333333" cat "textbook">
<title lang="eng">Intro. to Databases</title>
<price unit="usd">39.00</price>
</book>
</bookstore>
DTD
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book*)>
<!ELEMENT book (title, price)>
<!ATTLIST book isbn ID #REQUIRED
cat CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ATTLIST title lang CDATA #REQUIRED>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price unit CDATA #REQUIRED>
]>
Exercise 1
1) Find the title and isbn of non-fiction books with a price of more than 50 USD.
– /bookstore/book[attribute::cat!="fiction" and price>50.00]/title |
/bookstore/book[attribute::cat!="fiction" and price>50.00]/@isbn
2) Find average price of textbooks.
– sum(/bookstore/book[attribute::cat="textbook"]/price/number(text()))
div
count(/bookstore/book[attribute::cat textbook ]/price)3) Find the titles of textbooks on XML.
– /bookstore/book[attribute::cat="textbook" and contains(title,
"XML")]/title/text()
Exercise 2
1) Create a new document which contain only the isbn and title of textbooks.
<textbooks>
{ for $book in doc("bookstore.xml")//book
where $book/@cat="textbook"
return <textbook isbn="$book/@isbn">{$book/title}</textbook>
}
</textbooks>
Result:
<textbooks>
<textbook isbn="222222">
<title lang="eng">Learning XML</title>
</textbook>
<textbook isbn="333333">
<title lang="eng">Intro. to Databases</title>
</textbook>
</textbooks>
2) Find the title and price of the book with isbn = '222222'.
for $book in doc("bookstore.xml")//book
where $book[@isbn="222222"]
return <book>{ $book/title, $book/price}</book>
Result:
<book>
<title lang="eng">Learning XML</title>
<price unit="usd">69.95</price>
</book>
3) Produce a list of non-fictions with their title and price, sorted by price.
<nonfiction‐list>
{ for $book in doc("bookstore.xml")//book, $title in $book/title, $price
in $book/price
where $book/@cat!="fiction" order by $price/text()
return <nonfiction>{ $title, $price}</nonfiction>
}
</nonfiction‐list>
Result:
<nonfiction‐list>
<nonfiction>
<title lang="eng">Intro. to Databases</title>
<price unit="usd">39.00</price>
</nonfiction>
<nonfiction>
<title lang="eng">Learning XML</title>
<price unit="usd">69.95</price>
</nonfiction>
</nonfiction‐list>
4) Find the title of the text book with highest price.
<textbooks>
{ let $prices := doc("bookstore.xml")//book[@cat="textbook"]/price
let $max := max($prices)
return
<max‐price‐textbook price="{$max}">
{for $book in doc("bookstore.xml")//book
where $book/price = $max
return $book/title
}
</max‐price‐textbook>
}
</textbooks>
Result:
<textbooks>
<max‐price‐textbook price="69.95">
<title lang="eng">Learning XML</title>
</max‐price‐textbook>
</textbooks>
5) Organize books by categories.
<summary‐by‐category>
{ let $categories :=
for $category in doc("bookstore.xml")//book/@cat
return $category
for $cat in distinct‐values($categories)
return
<category id="{$cat}">{ for $book in doc("bookstore.xml")//book
where $book[@cat = $cat]
return $book }
</category>
}
</summary‐by‐category>
6) Reconstruct the document to produce the total price and count of books in each
category.
<price‐by‐category>
{ let $categories :=
for $category in doc("bookstore.xml")//book/@cat
return $category
for $cat in distinct‐values($categories)
return
<category id="{$cat}">
{ let $prices‐in‐cat := doc("bookstore.xml")//book[@cat=$cat]/price
return <price total="{sum($prices‐in‐cat)}"
count="{count($prices‐in‐cat)}"/>
}
</category>
}
</price‐by‐category>