"nonNegativeInteger", "positiveInteger", "nonPositiveInteger", "negativeInteger" Datatypes

This section describes the built-in primitive datatypes, 'nonNegativeInteger', 'positiveInteger', 'nonPositiveInteger' and 'negativeInteger', that represents integer numbers according signs and the zero value. Leading and trailing whitespaces are allowed and trimmed.

Also based on the "long" datatype, 4 other built-in datatypes are derived in XSD 1.1 to represent integer numbers within different ranges according to signs and the zero value: "nonNegativeInteger", "positiveInteger", "nonPositiveInteger" and "negativeInteger". Their definitions can be summarized as:

To verify these rules, I wrote this simple XSD document that uses "nonNegativeInteger", "positiveInteger", "nonPositiveInteger" and "negativeInteger" datatypes to declare XML elements:

<?xml version="1.1"?>
<!-- positive_negative_integer_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="Positive_Negative_Integer_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="NonNegativeInteger" type="xs:nonNegativeInteger"
        maxOccurs="unbounded"/>
      <xs:element name="PositiveInteger" type="xs:positiveInteger"
        maxOccurs="unbounded"/>
      <xs:element name="NonPositiveInteger" type="xs:nonPositiveInteger"
        maxOccurs="unbounded"/>
      <xs:element name="NegativeInteger" type="xs:negativeInteger"
        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"?>
<!-- positive_negative_integer_datatype_test.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<Positive_Negative_Integer_Datatype_Test>
<!-- 2 valid "NonNegativeInteger" elements -->
  <NonNegativeInteger>012345678901234567890123456</NonNegativeInteger>
  <NonNegativeInteger>000000000000000000000000000</NonNegativeInteger>

<!-- 1 invalid "NonNegativeInteger" elements -->
  <NonNegativeInteger>-1</NonNegativeInteger>

<!-- 1 valid "PositiveInteger" elements -->
  <PositiveInteger>012345678901234567890123456</PositiveInteger>

<!-- 2 invalid "PositiveInteger" elements -->
  <PositiveInteger>-1</PositiveInteger>
  <PositiveInteger> 0</PositiveInteger>

<!-- 2 valid "NonPositiveInteger" elements -->
  <NonPositiveInteger>-012345678901234567890123456</NonPositiveInteger>
  <NonPositiveInteger> 000000000000000000000000000</NonPositiveInteger>

<!-- 1 invalid "NonPositiveInteger" elements -->
  <NonPositiveInteger>1</NonPositiveInteger>

<!-- 1 valid "NegativeInteger" elements -->
  <NegativeInteger>-012345678901234567890123456</NegativeInteger>

<!-- 2 invalid "NegativeInteger" elements -->
  <NegativeInteger>1</NegativeInteger>
  <NegativeInteger>0</NegativeInteger>

</Positive_Negative_Integer_Datatype_Test>

When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get 6 groups errors for 6 invalid XML elements:

herong> java XsdSchemaValidator
^^^ positive_negative_integer_datatype_test.xsd
^^^ positive_negative_integer_datatype_test.xml

Error:
   Line number: 11
   Column number: 46
   Message: cvc-minInclusive-valid: Value '-1' is not facet-valid with
   respect to minInclusive '0' for type 'nonNegativeInteger'.

Error:
   Line number: 11
   Column number: 46
   Message: cvc-type.3.1.3: The value '-1' of element
   'NonNegativeInteger' is not valid.

Error:
   Line number: 17
   Column number: 40
   Message: cvc-minInclusive-valid: Value '-1' is not facet-valid with
   respect to minInclusive '1' for type 'positiveInteger'.

Error:
   Line number: 17
   Column number: 40
   Message: cvc-type.3.1.3: The value '-1' of element
   'PositiveInteger' is not valid.

Error:
   Line number: 18
   Column number: 40
   Message: cvc-minInclusive-valid: Value '0' is not facet-valid with
   respect to minInclusive '1' for type 'positiveInteger'.

Error:
   Line number: 18
   Column number: 40
   Message: cvc-type.3.1.3: The value ' 0' of element
   'PositiveInteger' is not valid.

Error:
   Line number: 25
   Column number: 45
   Message: cvc-maxInclusive-valid: Value '1' is not facet-valid with
   respect to maxInclusive '0' for type 'nonPositiveInteger'.

Error:
   Line number: 25
   Column number: 45
   Message: cvc-type.3.1.3: The value '1' of element
   'NonPositiveInteger' is not valid.

Error:
   Line number: 31
   Column number: 39
   Message: cvc-maxInclusive-valid: Value '1' is not facet-valid with
   respect to maxInclusive '-1' for type 'negativeInteger'.

Error:
   Line number: 31
   Column number: 39
   Message: cvc-type.3.1.3: The value '1' of element 'NegativeInteger'
   is not valid.

Error:
   Line number: 32
   Column number: 39
   Message: cvc-maxInclusive-valid: Value '0' is not facet-valid with
   respect to maxInclusive '-1' for type 'negativeInteger'.

Error:
   Line number: 32
   Column number: 39
   Message: cvc-type.3.1.3: The value '0' of element 'NegativeInteger'
   is not valid.

Failed with errors: 12

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 APIs

 XML Schema Language - Basics

 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

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB