Facets Supported in "decimal"

This section describes fundamental facets and constraining facets supported in 'decimal' and its derived built-in datatypes.

"decimal" is the another commonly used simple built-in datatype. We want to see how many facets are supported in "decimal".

The "decimal" datatype has the following settings for its fundamental facets defined:

    ordered = total
    bounded = false
    cardinality = countably infinite
    numeric = true

The "decimal" datatype supports the following constraining facets:

    whiteSpace = collapse
    pattern = undefined
    enumeration = undefined
    assertions = undefined
    maxInclusive = undefined
    maxExclusive = undefined
    minInclusive = undefined
    minExclusive = undefined
    totalDigits = undefined
    fractionDigits = undefined

datatypes derived from "decimal" also have the same list of fundamental and constraining facets.

Here is a sample schema, decimal_datatype_facet.xsd, that uses "decimal" and "positiveInteger" datatypes with different restriction facets:

<?xml version="1.1"?>
<!-- decimal_datatype_facet.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Decimal_Datatype_Facet">
  <xs:complexType>
    <xs:sequence>
     <xs:element name="orderLine" maxOccurs="unbounded">
      <xs:complexType>
     
       <!-- Using "maxInclusive" restriction facet -->
       <xs:attribute name="quantity">
        <xs:simpleType>
         <xs:restriction base="xs:positiveInteger">
          <xs:maxInclusive value="1000"/>
         </xs:restriction>
        </xs:simpleType>
       </xs:attribute>
     
       <!-- Using "totalDigits" and other restriction facets -->
       <xs:attribute name="price">
        <xs:simpleType>
         <xs:restriction base="xs:decimal">
          <xs:totalDigits value='5'/>
          <xs:fractionDigits value='2'/>
          <xs:minInclusive value='10.01'/>
          <xs:maxInclusive value='99.99'/>
         </xs:restriction>
        </xs:simpleType>
       </xs:attribute>
     
       <!-- Using "minInclusive" restriction facet -->
       <xs:attribute name="subTotal">
        <xs:simpleType>
         <xs:restriction base="xs:decimal">
          <xs:minInclusive value='10.01'/>
         </xs:restriction>
        </xs:simpleType>
       </xs:attribute>
     
      </xs:complexType>
     </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

Here is a sample XML document, decimal_datatype_facet.xml, to test the above XSD document:

<?xml version="1.1"?>
<!-- decimal_datatype_facet.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<Decimal_Datatype_Facet>
   
   <!-- Valid attribute values -->
   <orderLine
    quantity="3"
    price="19.99"
    subTotal="59.97"
   />
        
   <!-- Inalid attribute values -->
   <orderLine
    quantity="1003"
    price="19.9999"
    subTotal="9.99"
   />
</Decimal_Datatype_Facet>

When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get these errors:

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator 
   decimal_datatype_facet.xsd decimal_datatype_facet.xml
   
Error:
   Line number: 19
   Column number: 6
   Message: cvc-maxInclusive-valid: Value '1003' is not facet-valid 
   with respect to maxInclusive '1000' for type 
   '#AnonType_quantityorderLineDecimal_Datatype_Facet'.

Error:
   Line number: 19
   Column number: 6
   Message: cvc-attribute.3: The value '1003' of attribute 'quantity'
   on element 'orderLine' is not valid with respect to its type, 
   'null'.

Error:
   Line number: 19
   Column number: 6
   Message: cvc-fractionDigits-valid: Value '19.9999' has 4 fraction 
   digits, but the number of fraction digits has been limited to 2.

Error:
   Line number: 19
   Column number: 6
   Message: cvc-attribute.3: The value '19.9999' of attribute 'price' 
   on element 'orderLine' is not valid with respect to its type, 
   'null'.

Error:
   Line number: 19
   Column number: 6
   Message: cvc-minInclusive-valid: Value '9.99' is not facet-valid
   with respect to minInclusive '10.01' for type 
   '#AnonType_subTotalorderLineDecimal_Datatype_Facet'.

Error:
   Line number: 19
   Column number: 6
   Message: cvc-attribute.3: The value '9.99' of attribute 'subTotal'
   on element 'orderLine' is not valid with respect to its type, 
   'null'.

Failed with errors: 6

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 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

 Using Xerces2 Java API

 XML Schema Language - Basics

 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

Facets, Constraining Facets and Restriction Datatypes

 What Is Facet?

 List of Facets

 Constructing New Datatypes with Restriction Facets

 Facets Supported in "string"

 Facets Supported in "dateTime"

Facets Supported in "decimal"

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 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

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version