XQuery

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
format-integer(123, '0000') 0123 Adding leading zeros
format-integer(12345678, '#,##0') 12,345,678 Inserting commas between groups
format-integer(4, 'a') d Using other numbering systems (letters, Roman numerals, etc.)
format-integer(4, 'Ww') Four Using words
format-integer(15, '0;o') 15th Using ordinal numbers
Table 17-11. Examples of format-number
ExampleReturn valuePurpose
format-number(12, '0000.00') 0012.00 Padding with leading and trailing zeros
format-number(12345.6, '#,###.0') 12,345.6 Inserting commas between groups
format-number(-1, "#,##0.00;(#,##0.00)") (1.00) Using different formats for negative numbers
format-number(0.18, '0%') 18% Calculating percentages
format-number(12, 'Number: 0') Number: 12 Inserting other characters before or after
Datypic XQuery Services