XQuery

(pwalmsley@datypic.com)

ISBN: 0596006349

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

# Chapter 16: Working with numbers

Table 16-1. The number function
ExampleReturn value
`number(doc("prices.xml")//prod[1]/price)` `29.99`
`number(doc("prices.xml")//prod[1]/price/@currency)` `NaN`
`number("29.99")` `29.99`
`number(())` `NaN`
Table 16-2. Comparing numeric values
ExampleValue
`doc("prices.xml")//prod[3]/discount > 10` `false`
`doc("prices.xml")//prod[3]/discount gt 10` `Type error`
`doc("prices.xml")//prod[3]/discount > doc("prices.xml")//prod[1]/discount` `true` (it is comparing the string `3.99` to the string `10.00`)
`doc("prices.xml")//prod[3]/number(discount) > doc("prices.xml")//prod[1]/number(discount)` `false` (it is comparing the number 3.99 to the number 10.00)
`3 gt 2` `true`
`1 = 1.0` `true`
`xs:float("NaN") = xs:float("NaN")` `false`
`xs:string(xs:float("NaN")) = "NaN"` `true`
This table assumes that `prices.xml` is untyped, i.e., has not been validated with a schema.
Table 16-3. Examples of arithmetic expressions
ExampleValueValue type
`5 + 3` `8` `xs:integer`
`5 + 3.0` `8` `xs:decimal`
`5 + 3.0E0` `8` `xs:double`
`5 * 3` `15` `xs:integer`
`2 + 3 * 5` `17` `xs:integer`
`(2 + 3) * 5` `25` `xs:integer`
`-3 + 5` `2` `xs:integer`
`() + 3` `()` `N/A`
`doc("prices.xml")//prod[1]/price+5` `34.99` `xs:decimal`
`doc("prices.xml")//prod[1]/price-5` `()` `N/A`
`doc("prices.xml")//prod[1]/price - 5` `24.99` `xs:decimal`
Table 16-4. Examples of the `div` and `idiv` operators
ExampleValueValue type
`14 div 4` `3.5` `xs:double`
`14 idiv 4` `3` `xs:integer`
`-14 idiv 4` `-3` `xs:integer`
`14.0 div 3.5` `4.0` `xs:decimal`
`14.0 idiv 3.5` `4` `xs:integer`
`() div 3` `()` `N/A`
`14 div 0` Error (division by zero) `N/A`
`xs:float("14") div 0` `INF` `xs:float`
`xs:double("INF") div 2` `INF` `xs:double`
`xs:float("NaN") div 2` `NaN` `xs:float`
Table 16-5. Examples of the `mod` operator
ExampleValueValue type
`14 mod 4` `2` `xs:integer`
`-14 mod 4` `-2` `xs:integer`
`14 mod -4` `2` `xs:integer`
`14.9 mod 2.1` `0.2` `xs:decimal`
`14.5E1 mod 2E1` `5` `xs:double`
`xs:float("14") mod 0` `NaN` `xs:float`
`xs:double("INF") mod 2` `NaN` `xs:double`
`14 mod ()` `()` `N/A`
`14 mod xs:double("INF")` `14` `xs:double`