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.5compares$priceto thexs:decimalvalue25.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:floatvalue from a stringxs:decimal($prod/price), which constructs anxs:decimalvalue 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
sumfunction.
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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access