Chapter 3: Expressions: XQuery Building Blocks
Example 3-1. Conditional expression
for $prod in (doc("catalog.xml")/catalog/product)
return if ($prod/@dept = 'ACC')
then <accessoryNum>{data($prod/number)}</accessoryNum>
else <otherNum>{data($prod/number)}</otherNum>
Example 3-2. Conditional expression returning multiple expressions
for $prod in (doc("catalog.xml")/catalog/product)
return if ($prod/@dept = 'ACC')
then (<accessoryNum>{data($prod/number)}</accessoryNum>,
<accessoryName>{data($prod/name)}</accessoryName>)
else <otherNum>{data($prod/number)}</otherNum>
Example 3-3. Nested conditional expressions
for $prod in (doc("catalog.xml")/catalog/product)
return if ($prod/@dept = 'ACC')
then <accessory>{data($prod/number)}</accessory>
else if ($prod/@dept = 'WMN')
then <womens>{data($prod/number)}</womens>
else if ($prod/@dept = 'MEN')
then <mens>{data($prod/number)}</mens>
else <other>{data($prod/number)}</other>
Useful Function. between-inclusive (see also functx:between-inclusive)
declare namespace functx = "http://www.functx.com";
declare function functx:between-inclusive
($value as xs:anyAtomicType, $minValue as xs:anyAtomicType,
$maxValue as xs:anyAtomicType) as xs:boolean {
$value >= $minValue and $value <= $maxValue
};
(: Example call :)
let $prod := doc("catalog.xml")//product[1]
return functx:between-inclusive ($prod/number, 1, 500)
|