XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 1491915103

2nd edition, , O'Reilly Media, Inc.

Chapter 3: Expressions: XQuery Building Blocks

Please note that the book contains many inline examples and informal tables that are not provided here.

Table 3-2. General comparisons
ExampleValue
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 = "2" Error XPTY0004
(1, "a") = (2, "b") Error XPTY0004
Table 3-3. Value comparisons
ExampleValue
3 gt 4 false
"abc" lt "def" true
doc("catalog.xml")/catalog/product[4]/number lt 500 Error XPTY0004, if number is untyped or non-numeric
<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
() eq 1 ()
1 eq "2" Error XPTY0004
(1, 2) eq (1, 2) Error XPTY0004
Table 3-4. Node comparisons
ExampleValue
$n1 is $n2 false
$n1 is $n1 true
$n1 is doc("catalog.xml")//product[number = 563] true
$n1/@dept is $n2/@dept false
Example 3-1. Conditional expression
Query
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>
Results
<otherNum>557</otherNum>
<accessoryNum>563</accessoryNum>
<accessoryNum>443</accessoryNum>
<otherNum>784</otherNum>
Example 3-2. Conditional expression returning multiple expressions
Query
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>
Results
<otherNum>557</otherNum>
<accessoryNum>563</accessoryNum>
<accessoryName>Floppy Sun Hat</accessoryName>
<accessoryNum>443</accessoryNum>
<accessoryName>Deluxe Travel Bag</accessoryName>
<otherNum>784</otherNum>
Example 3-3. Nested conditional expressions
Query
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>
Results
<womens>557</womens>
<accessory>563</accessory>
<accessory>443</accessory>
<mens>784</mens>
Example 3-4. Switch expression similar to nested conditional expressions
Query
xquery version "3.0";
for $prod in (doc("catalog.xml")/catalog/product)
return switch($prod/@dept)
         case "ACC" return <accessory>{data($prod/number)}</accessory>
         case "WMN" return <womens>{data($prod/number)}</womens>
         case "MEN" return <mens>{data($prod/number)}</mens>
         default return <other>{data($prod/number)}</other>
Results
<womens>557</womens>
<accessory>563</accessory>
<accessory>443</accessory>
<mens>784</mens>
Table 3-5. Examples of the not function
ExampleReturn value
not(true()) false
not(12 > 0) false
not(doc("catalog.xml")/catalog/product) false if there is at least one product child of catalog in catalog.xml
not( () ) true
not("") true
Datypic XQuery Services