XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0596006349
1st edition, , O'Reilly Media, Inc.
Chapter 6: Selecting and joining using FLWORs
for $prod in doc("catalog.xml")//product let $prodDept := $prod/@dept where $prodDept = "ACC" or $prodDept = "WMN" return $prod/name
for $i in 1 to 3 return <oneEval>{$i}</oneEval>
for $i in (1, 2) for $j in ("a", "b") return <oneEval>i is {$i} and j is {$j}</oneEval>
for $i in (1, 2), $j in ("a", "b") return <oneEval>i is {$i} and j is {$j}</oneEval>
let $i := (1 to 3) return <oneEval>{$i}</oneEval>
let $doc := doc("catalog.xml") for $prod in $doc//product let $prodDept := $prod/@dept let $prodName := $prod/name where $prodDept = "ACC" or $prodDept = "WMN" return $prodName
for $prod in doc("catalog.xml")//product let $prodDept := $prod/@dept where $prod/number > 100 and starts-with($prod/name, "F") and exists($prod/colorChoices) and ($prodDept = "ACC" or $prodDept = "WMN") return $prod
some $dept in doc("catalog.xml")//product/@dept satisfies ($dept = "ACC")
every $dept in doc("catalog.xml")//product/@dept satisfies ($dept = "ACC")
not(some $dept in doc("catalog.xml")//product/@dept satisfies ($dept = "ACC"))
some $i in (1 to 3), $j in (10, 11) satisfies $j - $i = 7
let $prods := doc("catalog.xml")//product for $d in distinct-values($prods/@dept), $n in distinct-values($prods[@dept = $d]/number) return <result dept="{$d}" number="{$n}"/>
declare namespace functx = "http://www.functx.com"; declare function functx:distinct-deep ($nodes as node()*) as node()* { for $x in (1 to count($nodes)) let $node := $nodes[$x] let $restOfNodes := subsequence($nodes, $x + 1) return if (some $otherNode in $restOfNodes satisfies (deep-equal($otherNode, $node))) then () else $node }; (: Example call :) functx:distinct-deep(doc("catalog.xml")//product)
for $item in doc("order.xml")//item, $product in doc("catalog.xml")//product[number = $item/@num] return <item num="{$item/@num}" name="{$product/name}" quan="{$item/@quantity}"/>
for $item in doc("order.xml")//item, $product in doc("catalog.xml")//product where $item/@num = $product/number return <item num="{$item/@num}" name="{$product/name}" quan="{$item/@quantity}"/>
for $item in doc("order.xml")//item, $product in doc("catalog.xml")//product, $price in doc("prices.xml")//prices/priceList/prod where $item/@num = $product/number and $product/number = $price/@num return <item num="{$item/@num}" name="{$product/name}" price="{$price/price}"/>
for $product in doc("catalog.xml")//product return <product number="{$product/number}">{ attribute price {for $price in doc("prices.xml")//prices/priceList/prod where $product/number = $price/@num return $price/price} }</product>