XQuery

(pwalmsley@datypic.com)

ISBN: 1491915103

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

Chapter 17: Working with Numbers

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

Table 17-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 17-2. Comparing numeric values (assuming untyped input)
ExampleReturn value
doc("prices.xml")//prod[3]/discount > 10 false
doc("prices.xml")//prod[3]/discount gt 10 Error XPTY0004
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
Table 17-3. Examples of arithmetic expressions
ExampleReturn valueReturn 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:double
doc("prices.xml")//prod[1]/price-5 () N/A
doc("prices.xml")//prod[1]/price - 5 24.99 xs:double
Table 17-4. Examples of the div and idiv operators
ExampleReturn valueReturn type
14 div 4 3.5 xs:decimal
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 FOAR0001 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 17-5. Examples of the mod operator
ExampleReturn valueReturn 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
Table 17-10. Examples of format-integer
ExampleReturn valuePurpose