XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
"decimal" Datatype Values and Representations
This section describes the built-in primitive datatype, 'decimal' that represents signed decimal numbers. Leading and trailing whitespaces are allowed and trimmed.
The "decimal" datatype and its derived datatypes are frequently used built-in datatypes in XML documents. Let's take a closer look at the "decimal" datatype first.
"decimal" is a built-in primitive datatype designed to represent signed decimal numbers with these rules:
To verify these rules, I wrote this simple XSD document that uses "decimal" datatype to declare XML elements:
<?xml version="1.1"?> <!-- decimal_datatype_test.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Decimal_Datatype_Test"> <xs:complexType> <xs:sequence> <xs:element name="Decimal" type="xs:decimal" 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"?> <!-- decimal_datatype_test.xml - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <Decimal_Datatype_Test> <!-- 4 valid "Decimal" elements --> <Decimal> +123.456 </Decimal> <Decimal> -1234.456 </Decimal> <Decimal> 0000000123.4560000000 </Decimal> <Decimal> 0123456789012345678901234567890123456789.01234567890123456789 </Decimal> <!-- 3 invalid "Decimal" elements --> <Decimal>+ 123.456</Decimal> <Decimal>-1,234.456</Decimal> <Decimal>1.2345e2</Decimal> </Decimal_Datatype_Test>
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get 3 groups of errors for 3 invalid XML elements:
herong> java XsdSchemaValidator ^^^ decimal_datatype_test.xsd decimal_datatype_test.xml Error: Line number: 15 Column number: 31 Message: cvc-datatype-valid.1.2.1: '+ 123.456' is not a valid value for 'decimal'. Error: Line number: 15 Column number: 31 Message: cvc-type.3.1.3: The value '+ 123.456' of element 'Decimal' is not valid. Error: Line number: 16 Column number: 32 Message: cvc-datatype-valid.1.2.1: '-1,234.456' is not a valid value for 'decimal'. Error: Line number: 16 Column number: 32 Message: cvc-type.3.1.3: The value '-1,234.456' of element 'Decimal' is not valid. Error: Line number: 17 Column number: 30 Message: cvc-datatype-valid.1.2.1: '1.2345e2' is not a valid value for 'decimal'. Error: Line number: 17 Column number: 30 Message: cvc-type.3.1.3: The value '1.2345e2' of element 'Decimal' is not valid. Failed with errors: 6
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
►"decimal" Datatype Values and Representations
"integer" Datatype Values and Representations
"long", "int", "short" and "byte" Datatypes
"nonNegativeInteger", "positiveInteger", "nonPositiveInteger", "negativeInteger" Datatypes
""unsignedLong" and "unsignedShort" Datatypes
"dateTime" and Its Related Datatypes
Miscellaneous Built-in Datatypes
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