(: parse tests :) { for $sales in doc("sales-records.xml")/*/record let $pname := $sales/product-name group by $pname order by $pname return { sum($sales/qty) } } %%% { for $sales in doc("sales-records.xml")/*/record let $state := doc("stores.xml")/*/store[store-number = $sales/store-number]/state let $category := doc("products.xml")/*/product[name = $sales/product-name]/category group by $state, $category order by $state, $category return {$state, $category} {sum($sales/qty)} } %%% { for $sales in doc("sales-records.xml")/*/record let $state := doc("stores.xml")/*/store[store-number = $sales/store-number]/state, $product := doc("products.xml")/*/product[name = $sales/product-name], $category := $product/category, $revenue := $sales/qty * $product/price group by $state, $category order by $state, $category return {$state, $category} {sum($revenue)} } %%% { for $store in doc("stores.xml")/*/store let $state := $store/state group by $state order by $state return { for $product in doc("products.xml")/*/product let $category := $product/category group by $category order by $category return { for $sales in doc("sales-records.xml")/*/record[store-number = $store/store-number and product-name = $product/name] let $pname := $sales/product-name group by $pname order by $pname return } } } %%% { for $sales in doc("sales-records.xml")/*/record let $storeno := $sales/store-number group by $storeno order by $storeno return { for $s in $sales order by xs:int($s/qty) descending return } } %%% { for $sales in doc("sales-records.xml")/*/record let $storeno := $sales/store-number, $product := doc("products.xml")/*/product[name = $sales/product-name], $prd := $product, $profit := $sales/qty * ($prd/price - $prd/cost) group by $storeno let $total-store-profit := sum($profit) where $total-store-profit > 100 order by $total-store-profit descending return } %%% { for $book in doc("books.xml")/*/book for $author in $book/author group by $author order by $author return { for $b in $book order by $b/title return {fn:data($b/title)} } } %%% { for $book in doc("books.xml")/*/book let $author-list := fn:string-join($book/author, ', ') group by $author-list order by $author-list return { for $b in $book order by $b/title return {fn:data($b/title)} } } %%% declare variable $seq := fn:doc("arrange_rows.xml"); { for tumbling window $w in $seq/doc/* start at $x when fn:true() end at $y when $y - $x = 2 return { for $i in $w return } }
{data($i)}
%%% declare variable $seq := fn:doc("head_para.xml"); { for tumbling window $w in $seq/body/* start previous $s when $s[self::h2] end next $e when $e[self::h2] return
{ for $x in $w return {data($x)} }
}
%%% declare variable $seq := fn:doc("term_def_list.xml"); { for tumbling window $w in $seq/doc/* start $x when $x[self::dt] end $y next $z when $y[self::dd] and $z[self::dt] return { $w } } %%% declare variable $timesequence := fn:doc("temp_events.xml"); let $MAX_DIFF := 2 for sliding window $w in $timesequence/stream/event start $s_curr at $s_pos previous $s_prev when ($s_curr/@time ne $s_prev/@time) or (empty($s_prev)) only end next $e_next when $e_next/@time - $s_curr/@time gt $MAX_DIFF return avg( $w/@temp ) %%% declare variable $timesequence := fn:doc("temp_events.xml"); let $SMOOTH_CONST := 0.2 for sliding window $w in $timesequence/stream/event start at $s_pos when true() only end at $e_pos when $e_pos - $s_pos eq 2 return round-half-to-even($SMOOTH_CONST * data($w[3]/@temp) + (1 - $SMOOTH_CONST) * ( $SMOOTH_CONST * data($w[2]/@temp) + (1 - $SMOOTH_CONST) * data($w[1]/@temp) ), 2) %%% declare variable $seq := fn:doc("temp_events.xml"); for sliding window $w in $seq/stream/event start $s_curr when fn:true() only end next $next when $next/@time > $s_curr/@time + 3 return let $avg := fn:avg($w/@temp) where $avg * 2 lt xs:double($next/@temp) or $avg div 2 gt xs:double($next/@temp) return Outlier detected. Event id:{data($next/@time)} %%% declare variable $seq := fn:doc("mydoc.xml"); { for tumbling window $w in $seq/stream/event start $s when $s/person eq "Anton" and $s/direction eq "in" only end $e next $n when xs:dateTime($n/@time) - xs:dateTime($s/@time) gt xs:dayTimeDuration("PT1H") or ($e/person eq "Barbara" and $e/direction eq "in") or ($e/person eq "Anton" and $e/direction eq "out") where $e/person eq "Barbara" and $e/direction eq "in" return Barbara: Anton arrived 1h ago } %%% declare variable $seq := fn:doc("person_events.xml"); { for sliding window $w in $seq/stream/event start $s when $s/direction eq "in" only end $e when $s/person eq $e/person and $e/direction eq "out" return {$s/person} } %%% declare variable $seq := fn:doc("person_events.xml"); { for sliding window $w in $seq/stream/event start $s when $s/direction eq "in" only end $e when $s/person eq $e/person and $e/direction eq "out" let $person := $s/person let $workingTime := xs:dateTime($e/@time) - xs:dateTime($s/@time) group by $person return {$person} } %%% declare variable $seq := fn:doc("person_events.xml"); { for tumbling window $w in $seq/stream/event[direction eq "in"] start $s when fn:true() end next $e when xs:date( xs:dateTime($s/@time) ) ne xs:date( xs:dateTime($e/@time) ) let $date := xs:date(xs:dateTime($s/@time)) where not($w[person eq "Barbara"]) return Barbara did not come to work } %%% declare variable $seq := fn:doc("person_events.xml"); { for tumbling window $w in $seq/stream/event[direction eq "in"] start when true() only end next $x when $x/person eq "Clara" return { distinct-values(for $y in $w where (xs:dateTime($y/@time) + xs:dayTimeDuration("PT15M") ) ge xs:dateTime($x/@time) return $y/person) } } %%% declare variable $seq := fn:doc("person_events.xml"); { for tumbling window $w in $seq/stream/event[direction eq "in"] start $x when $x/person = ("Barbara", "Anton") end next $y when xs:dateTime($y/@time) - xs:dateTime($x/@time) gt xs:dayTimeDuration("PT30M") where $w[person eq "Anton"] and $w[person eq "Barbara"] return Anton and Barbara just arrived } %%% declare variable $seq := fn:doc("person_events.xml"); { for sliding window $w in $seq/stream/event start $s when true() end next $e when xs:dateTime($e/@time) - xs:dateTime($s/@time) gt xs:dayTimeDuration("PT1H") where count($w[person eq $s/person and direction eq "in"]) ge 3 return {fn:data($s/person)} is suspicious } %%% declare variable $rssfeed := fn:doc("rss.xml"); { for tumbling window $w in $rssfeed/rss/channel/item start $first when fn:true() end next $lookAhead when $first/author ne $lookAhead/author where count($w) ge 3 return { $w[1]/author } } %%% declare variable $rssfeed := fn:doc("rss.xml"); { for tumbling window $w in $rssfeed/rss/channel/item start $s_curr when true() end next $e_next when fn:day-from-dateTime(xs:dateTime($e_next/pubDate)) ne fn:day-from-dateTime(xs:dateTime($s_curr/pubDate)) return {xs:date(xs:dateTime($s_curr/pubDate))} { for $item in $w where fn:contains( xs:string($item/title), 'XQuery') return $item/title } } %%% declare variable $rssfeed := fn:doc("rss.xml"); { for tumbling window $w in $rssfeed/rss/channel/item start $s_curr when true() end next $e_next when fn:day-from-dateTime(xs:dateTime($e_next/pubDate)) ne fn:day-from-dateTime(xs:dateTime($s_curr/pubDate)) return {xs:date(xs:dateTime($s_curr/pubDate))} { for $a in fn:distinct-values($w/author) return { $w[author eq $a]/title } } } %%% declare variable $seq := fn:doc("cxml.xml"); { for sliding window $w in $seq/sequence/* start $cur previous $prev when day-from-dateTime($cur/@date) ne day-from-dateTime($prev/@date) or empty($prev) end $end next $next when day-from-dateTime(xs:dateTime($end/@date)) ne day-from-dateTime(xs:dateTime($next/@date)) return { let $companies := for $x in distinct-values($w/@billTo ) return {sum($w[@billTo eq $x]/@total)} let $max := max($companies) for $company in $companies where $company eq xs:untypedAtomic($max) return $company } } %%% declare variable $seq := fn:doc("cxml.xml"); { for sliding window $w in $seq/sequence/* start $s when $s[self::OrderRequest] end $e when $e/@orderID eq $s/@orderID and $e[self::ConfirmationRequest] and $e/@status eq "reject" or $e[self::ShipNotice] where $e[self::ShipNotice] return {xs:dateTime($e/@date) - xs:dateTime($s/@date) } } %%% declare variable $seq := fn:doc("cxml.xml"); { for sliding window $w in $seq/sequence/* start previous $wSPrev when $wSPrev[self::OrderRequest] end next $wENext when $wENext/@orderID eq $wSPrev/@orderID and ( $wENext[self::ConfirmationRequest] and $wENext/@status eq "reject") or $wENext[self::ShipNotice] where $wENext[self::ShipNotice] return { for sliding window $bundle in $w start $bSCur when $bSCur[self::OrderRequest] and $bSCur/@shipTo eq $wSPrev/@shipTo end $bECur next $bENext when $bECur/@orderID eq $bSCur/@orderID and ($bECur[self::ConfirmationRequest] and $bECur/@status eq "reject") or $bECur[self::ShipNotice] where empty($bENext) return $bSCur } } %%% { for $product in doc("products.xml")/*/product order by $product/name count $number return {$product/*} } %%% declare variable $seq := fn:doc("arrange_rows.xml"); { for tumbling window $w in $seq/doc/* start at $x when fn:true() end at $y when $y - $x = 2 count $rowNumber return { for $i in $w return } }
{$rowNumber}{data($i)}
%%% { for $sales in doc("sales-records.xml")/*/record let $name := $sales/product-name group by $name let $qty := sum($sales/qty) order by $qty descending count $count where $count <= 3 return } %%% { for $store in doc("stores.xml")/*/store for $sale allowing empty in doc("sales-records.xml")/*/record[ store-number eq $store/store-number] return } %%% { for $store in doc("stores.xml")/*/store for $product in doc("products.xml")/*/product for $sale allowing empty in doc("sales-records.xml")/*/record[ store-number eq $store/store-number and product-name eq $product/name] return } %%% module myfns = "http://www.example.com/function-library" try { { for $product in fn:doc("product-err.xml")//product return {$product/name} {$product/price - $product/cost} } } catch * { "An error occured, please ask your consultant for help." } %%% { for $product in fn:doc("product-err.xml")//product return try { {$product/name} {$product/price div $product/cost} } catch * ($errcode) { { ($product/name, "Error:", $errcode) } } } %%% declare namespace foo='http://foo.com'; declare function local:fib-recur($n as xs:integer) as xs:integer? { if ($n <0) then () else if ($n > 100) then fn:error(fn:QName('http://foo.com', 'ValueToBig'), 'Value too big') else if ($n = 0) then 0 else if ($n=1) then 1 else local:fib-recur($n - 1) + local:fib-recur($n - 2) }; { for $x in (3,1,1030,5) return try{ {local:fib-recur($x)} }catch foo:ValueToBig { Number to big } } %%% (: end of parse tests :)