Recitation_XQUERY

1. Phd.xml introduction
2. Running Saxonhe
1)CSE Server:
Follow the instruction: https://wiki.cse.buffalo.edu/services/content/saxon-he
Note:
java -cp saxon9he.jar net.sf.saxon.Query -t -s:samples/data/books.xml q:samples/query/books-to-html.xq > /home/csgrad/yyang25/tmp/books.html
Write result in your own directory, we cannot write file in util/saxon.It is read-only
in that folder.
2) Download saxonhe.
I put one named SaxonHE9513J.zip in general source. Unzip it and the command is
the same as above.
3.Putting all queries in one file:
see an example in general source named: product.sq.
3. XQUERY exercise (exercise 12.2.1 page 543)
XML file in general source: products.xml.
a) Find the Printer elements with a price less than 100.
for $x in doc("products.xml")/Products//Printer
where $x[@price <= 100] //type casting explicitly, $x[number(@price)<=100]
return (<result1>{$x}</result1>, '&#xa;'),
Note that the curly bracket is to force the evaluation of value, otherwise it will
treat the variable $x as plain text.
b) Find the Printer elements with a price less than 100, and produce the sequence
of these elements surrounded by a tag <CheapPrinter>.
for $y in doc("products.xml")/Products//Printer
where $y[@price <= 100]
order by $y[@price]
return (<CheapPrinter>{$y}</CheapPrinter>, '&#xa;'),
c) Find the names of the makers of both printers and laptops.
for $z in doc("products.xml")/Products/Maker
where $z[Printer|Laptop]
return if ($z[Printer])
then (<Maker name="{data($z/@name)}">{$z/Printer}</Maker>, '&#xa;')
else(
if ($z[Laptop])
then (<Maker name="{data($z/@name)}">{$z/Laptop}</Maker>, '&#xa;')
else()),
d) Find the names of the makers that produce at least two PC’s with a speed of
2.00 or more.
for $m in doc("products.xml")/Products/Maker
where count($m/PC[Speed > 2])>=2
return (<result4><name>{data($m/@name)}</name></result4>, '&#xa;'),
e) Find the makers such that every PC they produce have a price no more than
1000.
for $n in doc("products.xml")/Products/Maker
where every $t in $n/PC satisfies $t[@price <= 1000]
return (if(count($n[PC])>0)
then $n
else(), '&#xa;'),
f) Produce a sequence of elements of the form
<Laptop><Model>x</Model><Maker>y</Maker></Laptop>
for $a in doc("products.xml")/Products//Laptop
return
(<Laptop><Model>{data($a/@model)}</Model><Maker>{data($a/../@name)}</Mak
er></Laptop>, '&#xa;')