XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
"float" and "double" Datatype Values and Representations
This section describes the built-in primitive datatypes, 'float' and 'double' that represents single-precision (32-bit) and double-precision (64-bit) floating-point numbers. Leading and trailing whitespaces allowed and trimmed.
XSD 1.1 supports 2 built-in datatypes, "float" and "double", to represent floating-point numbers defined in the "IEEE 754-2008" standard with these rules:
To verify these rules, I wrote this simple XSD document that uses "float" and "double" datatypes to declare XML elements:
<?xml version="1.1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- float_double_datatype_test.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:element name="Float_Double_Datatype_Test"> <xs:complexType> <xs:sequence> <xs:element name="Float" type="xs:float" maxOccurs="unbounded"/> <xs:element name="Double" type="xs:double" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Here is a sample XML document that can be used to test these declarations:
<?xml version="1.1"?> <!-- float_double_datatype_test.xml - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <Float_Double_Datatype_Test> <!-- 3 valid "Float" elements --> <Float> 123 </Float> <Float> -1234567890.1234567890e1234567890 </Float> <Float> +1234567890.1234567890E-12345678901234567890 </Float> <Float> -INF </Float> <Float> NaN </Float> <!-- 3 invalid "Float" elements --> <Float> -1.23e1.23 </Float> <!-- 3 valid "Double" elements --> <Double> 1844674407370955161518446744073709551615 </Double> <Double> -1234567890.1234567890e1234567890 </Double> <Double> +1234567890.1234567890E-1234567890 </Double> <Double> INF </Double> <Double> NaN </Double> <!-- 3 invalid "Double" elements --> <Double> +INF </Double> </Float_Double_Datatype_Test>
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get 2 groups of errors for 2 invalid XML elements:
herong> java XsdSchemaValidator ^^^ float_double_datatype_test.xsd float_double_datatype_test.xml Error: Line number: 15 Column number: 35 Message: cvc-datatype-valid.1.2.1: '-1.23e1.23' is not a valid value for 'float'. (Exponential part must an "integer") Error: Line number: 15 Column number: 35 Message: cvc-type.3.1.3: The value ' -1.23e1.23 ' of element 'Float' is not valid. Error: Line number: 25 Column number: 31 Message: cvc-datatype-valid.1.2.1: '+INF' is not a valid value for 'double'. ("+" sign is not allowed on "INF") Error: Line number: 25 Column number: 31 Message: cvc-type.3.1.3: The value ' +INF ' of element 'Double' is not valid. Failed with errors: 4
Table of Contents
XML Editor and Schema Processor - XMLPad
Java API for XML Processing - JAXP
JAXP - XML Schema (XSD) Validation
Xerces2 Java Parser - Java API of XML Parsers
Introduction of XSD Built-in Datatypes
"string" and Its Derived Datatypes
"decimal" and Its Derived Datatypes
"dateTime" and Its Related Datatypes
►Miscellaneous Built-in Datatypes
"anyURI" Datatype Values and Representations
"QName" Datatype Values and Representations
"NOTATION" Datatype Values and Representations
"base64Binary" Datatype Values and Representations
"hexBinary" Datatype Values and Representations
►"float" and "double" Datatype Values and Representations
"boolean" Datatype Values and Representations
Facets, Constraining Facets and Restriction Datatypes
"simpleType" - Defining Your Own Simple Datatypes
Identity-Constraints: unique, key and keyref
Assertion as Custom Validation Rules
XML Schema Location and Namespace in XML Documents
Overriding Element Types in XML Documents