XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Chapter 6: Selecting and Joining Using FLWORs
Please note that the book contains many inline examples and informal tables that are not provided here.
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>
<oneEval>1</oneEval> <oneEval>2</oneEval> <oneEval>3</oneEval>
for $i in (1, 2) for $j in ("a", "b") return <oneEval>i is {$i} and j is {$j}</oneEval>
<oneEval>i is 1 and j is a</oneEval> <oneEval>i is 1 and j is b</oneEval> <oneEval>i is 2 and j is a</oneEval> <oneEval>i is 2 and j is b</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>
<oneEval>1 2 3</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}"/>
<result dept="WMN" number="557"/> <result dept="ACC" number="563"/> <result dept="ACC" number="443"/> <result dept="MEN" number="784"/>
for $item in doc("order.xml")//item, $prod in doc("catalog.xml")//product[number = $item/@num] return <item num="{$item/@num}" name="{$prod/name}" quan="{$item/@quantity}"/>
<item num="557" name="Fleece Pullover" quan="1"/> <item num="563" name="Floppy Sun Hat" quan="1"/> <item num="443" name="Deluxe Travel Bag" quan="2"/> <item num="784" name="Cotton Dress Shirt" quan="1"/> <item num="784" name="Cotton Dress Shirt" quan="1"/> <item num="557" name="Fleece Pullover" quan="1"/>
for $item in doc("order.xml")//item, $prod in doc("catalog.xml")//product where $item/@num = $prod/number return <item num="{$item/@num}" name="{$prod/name}" quan="{$item/@quantity}"/>
for $item in doc("order.xml")//item, $prod in doc("catalog.xml")//product, $price in doc("prices.xml")//prod where $item/@num = $prod/number and $prod/number = $price/@num return <item num="{$item/@num}" name="{$prod/name}" price="{$price/price}"/>
<item num="557" name="Fleece Pullover" price="29.99"/> <item num="563" name="Floppy Sun Hat" price="69.99"/> <item num="443" name="Deluxe Travel Bag" price="39.99"/> <item num="557" name="Fleece Pullover" price="29.99"/>
for $prod in doc("catalog.xml")//product return <product number="{$prod/number}">{ attribute price {for $price in doc("prices.xml")//prod where $prod/number = $price/@num return $price/price} }</product>
<product number="557" price="29.99"/> <product number="563" price="69.99"/> <product number="443" price="39.99"/> <product number="784" price=""/>
xquery version "3.0"; for $prod in doc("catalog.xml")//product for $price allowing empty in doc("prices.xml")//prices/priceList/prod[@num = $prod/number] return <product number="{$prod/number}" price="{$price/price}"/>