XQuery
Priscilla Walmsley (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.
Example | Value |
---|---|
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
|
Example | Value |
---|---|
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
|
Example | Value |
---|---|
$n1 is $n2
|
false
|
$n1 is $n1
|
true
|
$n1 is doc("catalog.xml")//product[number = 563]
|
true
|
$n1/@dept is $n2/@dept
|
false
|
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>
<otherNum>557</otherNum> <accessoryNum>563</accessoryNum> <accessoryNum>443</accessoryNum> <otherNum>784</otherNum>
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>
<otherNum>557</otherNum> <accessoryNum>563</accessoryNum> <accessoryName>Floppy Sun Hat</accessoryName> <accessoryNum>443</accessoryNum> <accessoryName>Deluxe Travel Bag</accessoryName> <otherNum>784</otherNum>
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>
<womens>557</womens> <accessory>563</accessory> <accessory>443</accessory> <mens>784</mens>
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>
<womens>557</womens> <accessory>563</accessory> <accessory>443</accessory> <mens>784</mens>
Example | Return 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
|