{ for $b in document("http://www.bn.com/bib.xml")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return { $b/title } } ; { for $b in document("http://www.bn.com/bib.xml")/bib/book, $t in $b/title, $a in $b/author return { $t } { $a } } ; { for $b in document("http://www.bn.com/bib.xml")/bib/book return { $b/title } { $b/author } } ; { for $a in distinct-values(document("http://www.bn.com/bib.xml")//author) return { $a } { for $b in document("http://www.bn.com/bib.xml")/bib/book where some $ba in $b/author satisfies deep-equal($ba,$a) return $b/title } } ; { for $b in document("http://www.bn.com/bib.xml")//book, $a in document("http://www.amazon.com/reviews.xml")//entry where $b/title = $a/title return { $b/title } { $a/price/text() } { $b/price/text() } } ; { for $b in document("http://www.bn.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 () } } ; { for $b in document("http://www.bn.com/bib.xml")//book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 order by $b/title return { $b/@year } { $b/title } } ; for $b in document("http://www.bn.com/bib.xml")//book let $e := $b/*[contains(string(.), "Suciu") and ends-with(local-name(.), "or")] where exists($e) return { $b/title } { $e } ; { for $t in document("books.xml")//(chapter | section)/title where contains($t/text(), "XML") return $t } ; { let $doc := document("prices.xml") for $t in distinct-values($doc//book/title) let $p := for $x in $doc//book[title = $t]/price return decimal($x) return { min($p) } } ; { for $b in document("http://www.bn.com/bib.xml")//book[author] return { $b/title } { $b/author } } { for $b in document("http://www.bn.com/bib.xml")//book[editor] return { $b/title } {$b/editor/affiliation} } ; { for $book1 in document("http://www.bn.com/bib.xml")//book, $book2 in document("http://www.bn.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 sequence-deep-equal($aut1, $aut2) return { $book1/title } { $book2/title } } ; define function toc($e as element ) as element* { let $n := local-name( $e ) return if ($n = "section") then
{ $e/@* } { toc($e/*) }
else if ($n = "title") then $e else () } { toc( document("book.xml")/book ) } ; { for $f in document("book.xml")//figure return
{ $f/@* } { $f/title }
}
; { count(document("book.xml")//section) }, { count(document("book.xml")//figure) } ; { count(document("book.xml")/book/section) } ; { for $s in document("book.xml")//section let $f := $s/figure return
} ; define function section_summary($s as element) as element {
{ $s/@* } { $s/title } { count($s/figure) } { for $ss in $s/section return section_summary($ss) }
} { for $s in document("book.xml")/book/section return section_summary($s) } ; for $s in document("report1.xml")//section[section.title = "Procedure"] return ($s//incision)[2]/instrument ; for $s in document("report1.xml")//section[section.title = "Procedure"] return ($s//instrument)[position()<=2] ; let $i2 := (document("report1.xml")//incision)[2] for $a in (document("report1.xml")//action)[. >> $i2][position()<=2] return $a//instrument ; for $p in document("report1.xml")//section[section.title = "Procedure"] where not(some $a in $p//anesthesia satisfies $a << ($p//incision)[1] ) return $p ; define function precedes($a as node, $b as node) as boolean { $a << $b and empty($a//node() intersect $b) } define function follows($a as node, $b as node) as boolean { $a >> $b and empty($b//node() intersect $a) } { let $proc := document("report1.xml")//section[section.title="Procedure"][1] for $n in $proc//node() where follows($n, ($proc//incision)[1]) and precedes($n, ($proc//incision)[2]) return $n } ; { let $proc := document("report1.xml")//section[section.title="Procedure"][1], $i1 := ($proc//incision)[1], $i2 := ($proc//incision)[2] for $n in $proc//node() except $i1//node() where $n >> $i1 and $n << $i2 return $n } ; { for $i in document("items.xml")//item_tuple where $i/start_date <= current-date() and $i/end_date >= current-date() and contains($i/description, "Bicycle") order by $i/itemno return { $i/itemno } { $i/description } } ; { for $i in document("items.xml")//item_tuple let $b := document("bids.xml")//bid_tuple[itemno = $i/itemno] where contains($i/description, "Bicycle") order by $i/itemno return { $i/itemno } { $i/description } { max(for $z in $b/bid return decimal($z)) } } ; { for $u in document("users.xml")//user_tuple for $i in document("items.xml")//item_tuple where $u/rating > "C" and $i/reserve_price > 1000 and $i/offered_by = $u/userid return { $u/name } { $u/rating } { $i/description } { $i/reserve_price } } ; { for $i in document("items.xml")//item_tuple where empty(document("bids.xml")//bid_tuple[itemno = $i/itemno]) return { $i/itemno } { $i/description } } ; { for $seller in document("users.xml")//user_tuple, $buyer in document("users.xml")//user_tuple, $item in document("items.xml")//item_tuple, $highbid in document("bids.xml")//bid_tuple where $seller/name = "Tom Jones" and $seller/userid = $item/offered_by and contains($item/description , "Bicycle") and $item/itemno = $highbid/itemno and $highbid/userid = $buyer/userid and $highbid/bid = max( for $x in document("bids.xml")//bid_tuple [itemno = $item/itemno]/bid return decimal($x) ) order by ($item/itemno) return { $item/itemno } { $item/description } { $highbid/bid } { $buyer/name } } ; { for $seller in unordered(document("users.xml")//user_tuple), $buyer in unordered(document("users.xml")//user_tuple), $item in unordered(document("items.xml")//item_tuple), $highbid in document("bids.xml")//bid_tuple where $seller/name = "Tom Jones" and $seller/userid = $item/offered_by and contains($item/description , "Bicycle") and $item/itemno = $highbid/itemno and $highbid/userid = $buyer/userid and $highbid/bid = max(for $x in unordered(document("bids.xml")//bid_tuple) [itemno = $item/itemno]/bid return decimal(data($x))) order by $item/itemno return { $item/itemno } { $item/description } { $highbid/bid } { $buyer/name } } ; { for $item in document("items.xml")//item_tuple let $b := document("bids.xml")//bid_tuple[itemno = $item/itemno] let $z := max(for $x in $b/bid return decimal($x)) where $item/reserve_price * 2 < $z return { $item/itemno } { $item/description } { $item/reserve_price } {$z } } ; let $allbikes := document("items.xml")//item_tuple [contains(description, "Bicycle") or contains(description, "Tricycle")] let $bikebids := document("bids.xml")//bid_tuple[itemno = $allbikes/itemno] return { max(for $x in $bikebids/bid return decimal($x)) } ; let $item := document("items.xml")//item_tuple [end_date >= date("1999-03-01") and end_date <= date("1999-03-31")] return { count($item) } ; { let $end_dates := document("items.xml")//item_tuple/end_date for $m in distinct-values(for $e in $end_dates return get-month-from-date($e)) let $item := document("items.xml") //item_tuple[get-year-from-date(end_date) = 1999 and get-month-from-date(end_date) = $m] order by $m return { $m } { count($item) } } ; { for $highbid in document("bids.xml")//bid_tuple, $user in document("users.xml")//user_tuple where $user/userid = $highbid/userid and $highbid/bid = max(for $x in document("bids.xml")//bid_tuple [itemno=$highbid/itemno]/bid return decimal($x)) order by $highbid/itemno return { $highbid/itemno } { $highbid/bid } { $user/name/text() } } ; let $highbid := max(for $x in document("bids.xml")//bid_tuple/bid return decimal($x)) return { for $item in document("items.xml")//item_tuple, $b in document("bids.xml")//bid_tuple[itemno = $item/itemno] where $b/bid = $highbid return { $item/itemno } { $item/description } { $highbid } } ; define function bid_summary () { for $i in distinct-values(document("bids.xml")//itemno) let $b := document("bids.xml")//bid_tuple[itemno = $i] return { $i } { count($b) } } { let $bid_counts := bid_summary(), $maxbids := max(for $x in $bid_counts/nbids return decimal($x)), $maxitemnos := $bid_counts[nbids = $maxbids] for $item in document("items.xml")//item_tuple, $bc in $bid_counts where $bc/nbids = $maxbids and $item/itemno = $bc/itemno return { $item/itemno } { $item/description } { $bc/nbids/text() } } ; { for $uid in distinct-values(document("bids.xml")//userid), $u in document("users.xml")//user_tuple[userid = $uid] let $b := document("bids.xml")//bid_tuple[userid = $uid] order by $u/userid return { $u/userid } { $u/name } { count($b) } { avg(for $x in $b/bid return decimal($x)) } } ; { for $i in distinct-values(document("bids.xml")//itemno) let $b := document("bids.xml")//bid_tuple[itemno = $i] let $avgbid := decimal(avg(for $x in $b/bid return decimal($x))) where count($b) >= 3 order by $avgbid descending return { $i } { $avgbid } } ; { for $u in document("users.xml")//user_tuple let $b := document("bids.xml")//bid_tuple[userid=$u/userid and bid>=100] where count($b) > 1 return { $u/name/text() } } ; { for $u in document("users.xml")//user_tuple let $b := document("bids.xml")//bid_tuple[userid = $u/userid] order by $u/userid return { $u/userid } { $u/name } { if (empty($b)) then inactive else active } } ; { for $u in document("users.xml")//user_tuple where every $item in document("items.xml")//item_tuple satisfies some $b in document("bids.xml")//bid_tuple satisfies ($item/itemno = $b/itemno and $u/userid = $b/userid) return $u/name } ; { for $u in document("users.xml")//user_tuple order by $u/name return { $u/name } { for $b in distinct-values(document("bids.xml")//bid_tuple [userid = $u/userid]/itemno) for $i in document("items.xml")//item_tuple[itemno = $b] let $descr := $i/description/text() order by $descr return { $descr } } } ; { input()//report//para } ; { input()//intro/para } ; { for $c in input()//chapter where empty($c/intro) return $c/section/intro/para } ; { (((input()//chapter)[2]//section)[3]//para)[2] } ; { input()//para[@security = "c"] } ; { for $s in input()//section/@shorttitle return { $s } } ; { for $i in input()//intro/para[1] return { substring(string($i), 1, 1) } } ; { input()//section[contains(string(.//title), "is SGML")] } ; { input()//section[contains(.//title/text(), "is SGML")] } ; { for $id in input()//xref/@xrefid return input()//topic[@topicid = $id] } ; { let $x := input()//xref[@xrefid = "top4"], $t := input()//title[. << $x] return $t[last()] } ; input()//news_item/title[contains(./text(), "Foobar Corporation")] ; define function partners($company as xs:string) as element* { let $c := document("company-data.xml")//company[name = $company] return $c//partner } let $foobar_partners := partners("Foobar Corporation") for $item in input()//news_item where some $t in $item//title satisfies (contains($t/text(), "Foobar Corporation") and some $partner in $foobar_partners satisfies contains($t/text(), $partner/text())) or some $par in $item//par satisfies (contains(string($par), "Foobar Corporation") and some $partner in $foobar_partners satisfies contains(string($par), $partner/text())) return { $item/title } { $item/date } ; define function partners($company as xs:string) as element* { let $c := document("company-data.xml")//company[name = $company] return $c//partner } for $item in input()//news_item, $c in document("company-data.xml")//company let $partners := partners($c/name) where contains(string($item), $c/name) and some $p in $partners satisfies contains(string($item), $p) and $item/news_agent != $c/name return $item ; for $item in input()//news_item where contains(string($item/content), "Gorilla Corporation") return { $item/title/text() }. { $item/date/text() }. { string(($item//par)[1]) } ; { for $n in distinct-values( for $i in (input()//* | input()//@*) return namespace-uri($i) ) return {$n} } ; declare namespace music = "http://www.example.org/music/records" { input()//music:title } ; declare namespace dt = "http://www.w3.org/2001/XMLSchema" { input()//*[@dt:*] } ; declare namespace xlink = "http://www.w3.org/1999/xlink" { for $hr in input()//@xlink:href return { $hr } } ; declare namespace music = "http://www.example.org/music/records" { input()//music:record[music:remark/@xml:lang = "de"] } ; declare namespace ma = "http://www.example.com/AuctionWatch" declare namespace anyzone = "http://www.example.com/auctioneers#anyzone" { input()//ma:Auction[@anyzone:ID]/ma:Schedule/ma:Close } ; declare namespace ma = "http://www.example.com/AuctionWatch" { for $a in document("data/ns-data.xml")//ma:Auction let $seller_id := $a/ma:Trading_Partners/ma:Seller/*:ID, $buyer_id := $a/ma:Trading_Partners/ma:High_Bidder/*:ID where namespace-uri($seller_id) = namespace-uri($buyer_id) return $a/ma:AuctionHomepage } ; declare namespace ma = "http://www.example.com/AuctionWatch" { for $s in input()//ma:Trading_Partners/(ma:Seller | ma:High_Bidder) where $s/*:NegativeComments = 0 return $s } ; define function one_level ($p as element) as element { { for $s in document("partlist.xml")//part where $s/@partof = $p/@partid return one_level($s) } } { for $p in document("partlist.xml")//part[empty(@partof)] return one_level($p) } ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" count( document("ipo.xml")//ipo:shipTo[. instance of element of type ipo:UKAddress] ) ; import schema "ipo.xsd" import schema "zips.xsd" declare namespace ipo="http://www.example.com/IPO" declare namespace zips="http://www.example.com/zips" define function zip-ok($a as element of type ipo:USAddress) as xs:boolean { some $i in document("zips.xml")/zips:zips/zips:row satisfies $i/zips:city = $a/ipo:city and $i/zips:state = $a/ipo:state and $i/zips:zip = $a/ipo:zip } ; import schema "ipo.xsd" import schema "postals.xsd" declare namespace ipo="http://www.example.com/IPO" declare namespace pst="http://www.example.com/postals" define function postal-ok($a as element of type ipo:UKAddress) as xs:boolean { some $i in document("postals.xml")/pst:postals/pst:row satisfies $i/pst:city = $a/ipo:city and xf:starts-with($a/ipo:postcode, $i/pst:prefix) } ; import schema "ipo.xsd" import schema "postals.xsd" import schema "zips.xsd" declare namespace ipo="http://www.example.com/IPO" declare namespace pst="http://www.example.com/postals" declare namespace zips="http://www.example.com/zips" define function postal-ok($a as element of type ipo:UKAddress) as xs:boolean { some $i in document("postals.xml")/pst:postals/pst:row satisfies $i/pst:city = $a/ipo:city and starts-with($a/ipo:postcode, $i/pst:prefix) } define function zip-ok($a as element of type ipo:USAddress) as xs:boolean { some $i in document("zips.xml")/zips:zips/zips:row satisfies $i/zips:city = $a/ipo:city and $i/zips:state = $a/ipo:state and $i/zips:zip = $a/ipo:zip } define function address-ok($a as element of type ipo:Address) as xs:boolean { typeswitch ($a) case $zip as element of type ipo:USAddress return zip-ok($zip) case $postal as element of type ipo:UKAddress return postal-ok($postal) default return false() } for $p in document("ipo.xml")//ipo:purchaseOrder where not( address-ok($p/ipo:shipTo) and address-ok($p/ipo:billTo)) return $p ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" define function names-match( $s as element ipo:shipTo context ipo:purchaseOrder, $b as element ipo:billTo context ipo:purchaseOrder ) as xs:boolean { $s/ipo:name = $b/ipo:name } for $p in document("ipo.xml")//ipo:purchaseOrder where not( names-match( $p/ipo:shipTo, $p/ipo:billTo ) ) return $p ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" for $p in document("ipo.xml")//ipo:purchaseOrder, $s in $p/ipo:shipTo where not( $s instance of element of type ipo:USAddress) and exists( $p//ipo:USPrice ) return $p ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" define function comment-text($c as element ipo:comment) as xs:string { string( $c ) } for $p in document("ipo.xml")//ipo:purchaseOrder, $t in comment-text( $p//ipo:shipComment ) where $p/ipo:shipTo/ipo:name="Helen Zoe" and $p/ipo:orderDate = date("1999-12-01") return $t ; for $p in document("ipo.xml")//ipo:purchaseOrder where $p/ipo:shipTo/ipo:name="Helen Zoe" and $p/@orderDate = date("1999-12-01") return comment-text( $p//ipo:customerComment ) ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" for $p in document("ipo.xml")//ipo:purchaseOrder where $p/ipo:shipTo/ipo:name="Helen Zoe" and $p/@orderDate = date("1999-12-01") return ($p//ipo:customerComment | $p//ipo:shipComment | $p//ipo:comment ) ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" define function comments-for-element( $e as element ) as ipo:comment* { let $c := $e/(ipo:customerComment | ipo:shipComment | ipo:comment) return $c } ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" define function deadbeat( $b as element ipo:billTo of type ipo:USAddress ) as xs:boolean { $b/ipo:name = document("http://www.usa-deadbeats.com/current")/deadbeats/row/name } ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" define function total-price( $i as element ipo:item* ) as xs:decimal { let $subtotals := for $s in $i return $s/ipo:quantity * $s/ipo:USPrice return sum( $subtotals ) } ; import schema "ipo.xsd" declare namespace ipo="http://www.example.com/IPO" for $p in document("ipo.xml")//ipo:purchaseOrder where $p/ipo:shipTo/ipo:name="Helen Zoe" and $p/ipo:orderDate = date("1999-12-01") return total-price($p//ipo:item)