Constructing Numeric Values
How does a value become "numeric"? As with any type, a value may be assigned one of the numeric types in a number of ways, for example:
It may be selected from an input document that has a schema declaring it to have a numeric type.
It may be a numeric literal value that appears in the query and is not surrounded by quotes. For example,
$price > 25.5
compares$price
to thexs:decimal
value25.5
.It may be the result of a function that returns a number, such as
count($products)
, which returns anxs:integer
.It may be the result of one of the standard constructor functions, such as:
xs:float("25.5E3")
, which constructs anxs:float
value from a stringxs:decimal($prod/price)
, which constructs anxs:decimal
value from an element
It may be the result of an explicit cast, such as
$prod/price cast as xs:decimal
.It may be cast automatically when it is passed to a function, such as the
sum
function.
The number Function
In addition to the standard type constructors, the number
function is useful for telling the processor to treat a node or atomic value as a number, regardless of its declared type (if any). It returns that argument cast as an xs:double
. If no argument is provided, the number
function uses the context node.
One difference between using the number
function and the xs:double
constructor is that the number
function returns the xs:double
value NaN
in the case that the value cannot be cast to a numeric value, whereas the xs:double
constructor throws an error. ...
Get XQuery now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.