XML technologies / XML Schema / Using predefined simple data types / Numeric data types

(Excerpt from “XML Schema” by Eric van der Vlist)

The numeric data types are based on four primitive data types: xs:decimal for all decimal types, including integer types, which are considered decimals without fractional digits, xs:double and xs:float for double- and single-precision floating-point numbers respectively, and xs:boolean for Boolean values. Whitespace is collapsed for all these data types.

The data types discussed on this page are shown in the following figure.

 

Numeric data types

Figure: Numeric data types

Decimal types

All decimal types derive from the primitive type xs:decimal. Together, they form a set of predefined types covering the most common uses.

xs:decimal

This data type represents decimal numbers. They may have any number of digits, since the type imposes no limit. However, because an XML document has an arbitrary but finite length, the number of digits in the lexical representation of a value of type xs:decimal must be finite. Although the number of digits is unlimited, the next chapter will show how schema authors can define custom types with a limited number of digits when necessary.

Leading zeros and trailing zeros after the decimal separator have no significance and may be removed. The decimal separator is always a period (“.”); a sign (“+”, “–”) may precede the number. All characters other than the ten digits are forbidden, including whitespace. Exponential notation (“E+2”) is also forbidden here and is reserved exclusively for floating-point data types.

Valid values for xs:decimal include:

123.456 +1234.456 –1234.456 –.456 –456

The following values are invalid:

1 234.456 (spaces are forbidden)
1234.456E+2 (exponential notation, “E+2”, is forbidden)
+ 1234.456 (spaces are forbidden)
+1,234.456 (thousands separators are forbidden)

xs:integer is the only predefined data type derived directly from xs:decimal .

xs:integer

This integer data type is a subset of xs:decimal, representing numbers with no fractional digits in either the lexical or value space. Accepted characters are limited to the ten digits and an optional initial sign. Like its base type, xs:integer imposes no limit on the number of digits, and leading zeros are not significant.

Valid values for xs:integer include:

123456 +00000012 –1 –456

The following values are invalid:

1 234 (spaces are forbidden)
1. (decimal separators are forbidden)
+1,234 (thousands separators are forbidden)

xs:integer has given rise to three derived data types: xs:nonPositiveInteger and xs:nonNegativeInteger , which still allow an unlimited length, and xs:long, which fits into a 64-bit word.

xs:nonPositiveInteger and xs:negativeInteger

The W3C XML Schema Working Group thought that using the somewhat awkward negative wording as a name made it clearer that “0” is included. They therefore used xs:nonPositiveInteger for numbers that may be negative or zero. xs:negativeInteger derives from xs:nonPositiveInteger to represent strictly negative integers. Both these data types allow integers of arbitrary length.

xs:nonNegativeInteger and xs:positiveInteger

Correspondingly, xs:nonNegativeInteger represents integers that are positive or zero, while xs:positiveInteger derives from this type. The unsigned branch of the family, xs:unsignedLong, xs:unsignedInt, xs:un-signedShort and xs:unsignedByte, also derives from xs:nonNegativeInteger.

xs:long, xs:int, xs:short and xs:byte

The data types seen so far have no length limit, which is not particularly microprocessor-friendly. This subfamily represents signed integers that fit into words of 8, 16, 32 and 64 bits respectively. xs:long is defined as all integers between –9223372036854775808 and 9223372036854775807, that is, values storable in a 64-bit word. The same process is repeated to derive xs:int with a range from –2147483648 to 2147483647, 32 bits, xs:short with a range from –32768 to 32767, 16 bits, and xs:byte with a range from –128 to 127, 8 bits.

xs:unsignedLong, xs:unsignedInt, xs:unsignedShort and xs:unsignedByte

The last predefined integer data types form the subfamily of unsigned, that is, nonnegative, integers that fit into words of 8, 16, 32 and 64 bits. xs:unsignedLong is defined for integers ranging from 0 to 18446744073709551615, that is, values storable in a 64-bit word. The same process is repeated to derive xs:unsignedInt with a range from 0 to 4294967295, 32 bits, xs:unsignedShort with a range from 0 to 65535, 16 bits, and xs:unsignedByte with a range from 0 to 255, 8 bits.

Floating-point data types

xs:float and xs:double

Both xs:float and xs:double are primitive data types representing IEEE single-precision, 32-bit, and double-precision, 64-bit, floating-point types respectively. Values are stored as a mantissa and an exponent of a power of two (m × 2^e), allowing a wide range of numbers in fixed-length storage. Fortunately, the lexical space does not require powers of two—in fact it does not allow them—but instead permits traditional exponential notation using integer powers of ten. Since the value spaces, powers of two, do not exactly match lexical-space values, powers of ten, the Recommendation specifies taking the nearest available value. This approximate correspondence places floating-point types in the realm of approximation. Most floating-point values cannot be considered exact, only approximations.

These types also accept several “special values”: positive zero (0), negative zero (–0), which is smaller than 0 but greater than any negative value; infinity (INF), greater than any other value; negative infinity (–INF), smaller than any other floating-point value; and “not a number” (NaN).

Valid values for xs:float and xs:double include:

123.456 +1234.456 –1.2344e56 –.45E–6 INF –INF NaN

The following values are invalid:

1234.4E 56 (spaces are forbidden)
1E+2.5 (the decimal exponent must be an integer)
+INF (positive infinity must not have a sign)
NAN (special values are case-sensitive)

xs:boolean

xs:boolean

This is a primitive data type accepting the values true and false, or 1 and 0.

   

 

 

 

back to top