Commit a7737870 authored by Pietro Abate's avatar Pietro Abate

[r2006-04-21 16:24:33 by beppe] Empty log message

Original author: beppe
Date: 2006-04-21 16:24:35+00:00
parent 8088e41c
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
-----------------------------Q1-----------------------------
List books published by Addison-Wesley after 1991, including their year and title.
------------------------------------------------------------
<bib>
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
where $b/publisher = "Addison-Wesley" and $b/@year > 1991
return
<book year="{ $b/@year }">
{ $b/title }
</book>
}
</bib>
*)
<bib>
select <book year=y>([b]/Title)
from b in [doc]/<book .. >[_* <publisher>"Addison-Wesley";_] ,
y in [b]/@year
where 1991<<int_of(y);;
<bib>
select <book year=y> t
from <bib>[b::Book*] in [doc],
<book year=y .. >[t::Title _+ <publisher>"Addison-Wesley";_] in b
where 1991 << int_of(y);;
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
--------------------------------Q11------------------------------------------
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.
-------------------------------------------------------------------------------
<bib>
{
for $b in doc("http://bstore1.example.com/bib.xml")//book[author]
return
<book>
{ $b/title }
{ $b/author }
</book>
}
{
for $b in doc("http://bstore1.example.com/bib.xml")//book[editor]
return
<reference>
{ $b/title }
{$b/editor/affiliation}
</reference>
}
</bib>
*)
<bib>((
select <book>(([b]/Title) @ ([b]/Author))
from b in [doc]/<book ..>[_ Author;_])
@
(select <reference>(([b]/Title) @ ([b]/Editor/Affiliation))
from b in [doc]/<book ..>[_ Editor;_]
));;
<bib>((
select <book>([t] @ la)
from <bib>[b::Book+] in [doc],
<book ..>[t&Title la::Author+ ;_] in b
)@(
select <reference>[t aff]
from <bib>[b::Book+] in [doc],
<book ..>[t&Title _* <editor>[_* aff&Affiliation] ;_] in b));;
<bib>
select <(tag)>([t] @ [a1] @a2)
from <book ..>[t&Title ((tag:=`book)&a1&Author a2::Author* | (tag:=`reference)&a1&Editor a2::Editor*) _*] in [doc]/Book;;
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
---------------------------------------------Q12----------------------------------------
Find pairs of books that have different titles but the same set of authors (possibly in a different order).
------------------------------------------------------------------------------------------
<bib>
{
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
<book-pair>
{ $book1/title }
{ $book2/title }
</book-pair>
}
</bib>
*)
flatten(
select select <book-pair>[!([b1]/Title) !([b2]/Title) ]
from a in [b2]/Author
where (member(a,[b1]/Author))
from b1 in [doc]/Book,
b2 in [doc]/Book
where not([b1]/Title = [b2]/Title)
)
(*
reste a retirer les doublons. besoin de l oprateur order pour ca.
*)
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
---------------------------Q2---------------------------------
Create a flat list of all the title-author pairs, with each pair enclosed in a "result" element.
--------------------------------------------------------------
<results>
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book,
$t in $b/title,
$a in $b/author
return
<result>
{ $t }
{ $a }
</result>
}
</results>
*)
<results>
select <result>([b]/Title @ [a])
from b in [doc]/Book,
a in [b]/Author ;;
<results>
select <result>[t a]
from <bib>[b::Book+] in [doc],
<book ..>[t&Title la::Author+ ;_] in b,
a in la ;;
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
---------------------------Q3---------------------------------
For each book in the bibliography, list the title and authors, grouped inside a "result" element.
--------------------------------------------------------------
<results>
{
for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
return
<result>
{ $b/title }
{ $b/author }
</result>
}
</results>
*)
<results>
select <result> ([b]/Title @ [b]/Author )
from b in [doc]/Book;;
<results>
select <result>(t @ a)
from <bib>[b::Book*] in [doc],
<book ..>[t::Title (a::Author+|_)* ;_] in b
;;
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
--------------------------------Q4------------------------------
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.
----------------------------------------------------------------
<results>
{
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
<result>
<author>
<last>{ $last }</last>
<first>{ $first }</first>
</author>
{
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
}
</result>
}
</results>
*)
<results>
select <result> ([a] @
flatten(select [b]/Title
from b in [doc]/Book
where member(a,[b]/Author) ))
from a in distinct_values( [doc]/Book/Author );;
<results>
select <result>([a] @(select t
from <bib>[l::Book+] in [doc],
<book ..>[t&Title a2::Author+ ;_] in l
where member(a,a2) ))
from a in distinct_values(
select a
from <bib>[b::Book+] in [doc],
<book ..>[Title la::Author+ ;_ ] in b,
a in la);;
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]
<publisher>['Kluwer Academic Publishers']
<price>['129.95']
]];;
(*
---------------------------Q5------------------------------------
For each book found at both bstore1.example.com and bstore2.example.com,
list the title of the book and its price from each source.
-----------------------------------------------------------------
<books-with-prices>
{
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
<book-with-prices>
{ $b/title }
<price-bstore2>{ $a/price/text() }</price-bstore2>
<price-bstore1>{ $b/price/text() }</price-bstore1>
</book-with-prices>
}
</books-with-prices>
*)
type Reviews =<reviews>[Entry*];;
type Entry = <entry> [ Title Price Review];;
(*type Title = <title>[PCDATA];;*)
(*type Price= <price>[PCDATA];;*)
type Review =<review>[PCDATA];;
let amazon=
<reviews>[
<entry>[
<title>['Data on the Web']
<price>['34.95']
<review>
['A very good discussion of semi-structured database
systems and XML.']
]
<entry>[
<title>['Advanced Programming in the Unix environment']
<price>['65.95']
<review>
['A clear and detailed discussion of UNIX programming.']
]
<entry>[
<title>['TCP/IP Illustrated']
<price>['65.95']
<review>
['One of the best books on TCP/IP.']
]
];;
<books-with-prices>
select <book-with-price>[t1 <price-amazon>([p2]/Char) <price-bn>([p1]/Char)]
from b in [doc]/Book ,
y in [b]/@year,
t1 in [b]/Title,
e in [amazon]/Entry,
t2 in [e]/Title,
p2 in [e]/Price,
p1 in [b]/Price
where t1=t2;;
<books-with-prices>
select <book-with-price>[t2 <price-amazon>p2 <price-bn>p1 ]
from <bib>[b::Book*] in [doc],
<book year=y ..>[t1&Title _* <price>p1] in b,
<reviews>[e::Entry*] in [amazon],
<entry>[t2&Title <price>p2 ;_] in e
where t1=t2;;
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']
<first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="1992">[
<title>['Advanced Programming in the Unix environment']
<author>[<last>['Stevens'] <first>['W.']]
<publisher>['Addison-Wesley']
<price>['65.95']
]
<book year="2000">[
<title>['Data on the Web']
<author>[<last>['Abiteboul'] <first>['Serge']]
<author>[<last>['Buneman'] <first>['Peter']]
<author>[<last>['Suciu'] <first>['Dan']]
<publisher>['Morgan Kaufmann Publishers']
<price>['39.95']
]
<book year="1999">[
<title>['The Economics of Technology and Content for Digital TV']
<editor>[
<last>['Gerbarg'] <first>['Darcy']
<affiliation>['CITI']
]