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 the xs:decimal value 25.5.

  • It may be the result of a function that returns a number, such as count($products), which returns an xs:integer.

  • It may be the result of one of the standard constructor functions, such as:

    • xs:float("25.5E3"), which constructs an xs:float value from a string

    • xs:decimal($prod/price), which constructs an xs: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. Table 16-1 shows some examples that use the number function.

Table 16-1. The number function

Example

Return 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

Numeric Type Promotion

If an operation, such as a comparison or arithmetic operation, is performed on values of two different primitive numeric types, one value is promoted to the type of the other value. Specifically, an xs:decimal value can be promoted to xs:float or xs:double, and an xs:float value to xs:double. For example, the expression 10 + 1.2E0 adds a decimal number to a floating-point number. The xs:decimal number (1.0) is promoted to xs:double before the expression is evaluated.

Numeric type promotion happens automatically in arithmetic expressions and comparison expressions. It is also used in calls to functions that expect numeric values. For example, if a function expects an xs:double value, you can pass it an xs:decimal value, and xs:decimal will be promoted to xs:double.

In addition to these specific promotion rules, any numeric value can be treated as if it has its type's base type or any ancestor type. This is known as subtype substitution. For example, if in your schema you define a type myDecimal that is derived by restriction from xs:decimal, a myDecimal value can be added to an xs:decimal value, returning an xs:decimal value. This rule also applies to built-in types. For example, since xs:integer is derived from xs:decimal, an xs:integer value can be used anywhere an xs:decimal value is expected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.145