ISBN: 0596006349

1st edition, , O'Reilly Media, Inc.

Chapter 3: Expressions: XQuery building blocks

Table 3-2. General comparisons
doc("catalog.xml")/catalog/product[2]/name = 'Floppy Sun Hat' true
doc("catalog.xml")/catalog/product[4]/number < 500 false
1 > 2 false
() = (1, 2) false
(2, 5) > (1, 3) true
(1, "a") = (2, "b") Type error
Table 3-3. Value comparisons
3 gt 4 false
"abc" lt "def" true
doc("catalog.xml")/catalog/product[4]/number lt 500 Type error, if number is untyped or nonnumeric
<a>3</a> gt <z>2</z> true
<a>03</a> gt <z>2</z> false, since a and z are untyped and treated like strings
(1, 2) eq (1, 2) Type error
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)
Table 3-4. Node comparisons
$n1 is $n2 false
$n1 is $n1 true
doc("catalog.xml")/catalog/product[1] is doc("catalog.xml")//product[number = 557] true
doc("catalog.xml")/catalog/product[2]/@dept is doc("catalog.xml")/catalog/product[3]/@dept false
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>,
       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>
Datypic XQuery Services
Table 3-5. Examples of the not function
ExampleReturn value
not(true()) false
not($numItems > 0) false if $numItems > 0
not(doc("catalog.xml")/catalog/ product) false if there is at least one product child of catalog in catalog.xml
not(()) true
not("") true