($x div $y) + xsd:decimal($z) ; xf:error(xf:concat("Unexpected value ", $v)) ; declare namespace foo = "http://example.org" Lentils ; {-- Error: multiple declarations of namespace 'xx' --} declare namespace xx = "http://example.org/foo" declare namespace xx = "http://example.org/bar" //xx:bing ; {-- Error: use of undeclared namespace prefix --} //xx:bing ; { //foo:bing } ; declare namespace xx = "http://example.org" let $i := Lentils return $i/xx:bing ; Lentils ; import schema "http://www.w3.org/1999/xhtml" at "http://example.org/xhtml/xhtml.xsd" declare namespace xhtml = "http://www.w3.org/1999/xhtml" document("aspect.xhtml")//xhtml:table ; import schema namespace xhtml="http://www.w3.org/1999/xhtml" at "http://example.org/xhtml/xhtml.xsd" document("aspect.xhtml")//xhtml:table ; define function price(element product in type catalog $p) returns element USPrice in type catalog/product { $p/USPrice } ; define function summary(element employee* $emps) returns element dept* { for $d in distinct-values($emps/deptno) let $e := $emps[deptno = $d] return {$d} {count($e)} {sum($e/salary)} } summary(document("acme_corp.xml")//employee[location = "Denver"]) ; define function depth(element $e) returns xs:integer { {-- An empty element has depth 1 --} {-- Otherwise, add 1 to max depth of children --} if (empty($e/*)) then 1 else max(for $c in $e/* return depth($c)) + 1 } depth(document("partlist.xml")) ; { for $i in document("catalog.xml")//item, $p in document("parts.xml")//part[partno = $i/partno], $s in document("suppliers.xml")//supplier[suppno = $i/suppno] return { $p/description, $s/suppname, $i/price } sort by(description, suppname) } ; for $s in document("suppliers.xml")//supplier return { $s/suppname, for $i in document("catalog.xml")//item [suppno = $s/suppno], $p in document("parts.xml")//part [partno = $i/pno] return $p/description sort by(.) } sort by(suppname) ; { for $s in document("suppliers.xml")//supplier return { $s/suppname, for $i in document("catalog.xml")//item [suppno = $s/suppno], $p in document("parts.xml")//part [partno = $i/partno] return { $p/description, $i/price } sort by (description) } sort by (suppname) , {-- parts that have no supplier --} { for $p in document("parts.xml")//part where empty(document("catalog.xml")//item [partno = $p/partno] ) return $p/description sort by (.) } } ; for $pn in distinct-values(document("catalog.xml")//partno) let $i := document("catalog.xml")//item[partno = $pn] where count($i) >= 3 return {$pn} {avg($i/price)} sort by(partno) ; let $proc := input()//procedure[1] for $i in $proc//action where $i >> ($proc//incision)[1] and $i << ($proc//incision)[2] return $i ; define function precedes(node $a, node $b) returns boolean { $a << $b and empty($a//node() intersect $b) } ; define function follows(node $a, node $b) returns boolean { $a >> $b and empty($b//node() intersect $a) } ; let $proc := input()//procedure[1] for $i in $proc//instrument where precedes(($proc//incision)[1], $i) and precedes($i, ($proc//incision)[2]) return $i ; for $p in input()//procedure where some $i in $proc//incision satisfies empty($proc//anesthesia[. << $i]) return $p ; let $intro := input()//h2[text()="Introduction"], $next-h := input()//(h1|h2)[. >> $intro][1] return
{ $intro, if (empty($next-h)) then //node()[. >> $intro] else //node()[. >> $intro and . << $next-h] }
; define function sections-and-titles(node $n) returns node? { if (xf:local-name($n) = "section") then element { xf:local-name($n) } { for $c in $n/* return sections-and-titles($c) } else if (xf:local-name($n) = "title") then $n else ( ) } ; sections-and-titles(document("cookbook.xml")) ; define function swizzle(node $n) returns node { if ($n instance of attribute and local-name($n) = "color") then element color { string($n) } else if ($n instance of element and local-name($n) = "size") then attribute size { string($n) } else if ($n instance of element) then element { local-name($n) } { for $c in $n/* return swizzle($c) } else $n } ; swizzle(document("plans.xml"))