XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

"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:

• The value space of "nonNegativeInteger" is all positive integer numbers and zero. Negative integers are excluded.
• The value space of "positiveInteger" is all positive integer numbers. Zero and negative integers are excluded.
• The value space of "nonPositiveInteger" is all negative integer numbers and zero. Positive integers are excluded.
• The value space of "negativeInteger" is all negative integer numbers. Zero and positive integers are excluded.
• Lexical spaces of "nonNegativeInteger", "positiveInteger", "nonPositiveInteger" and "negativeInteger" are all possible values in their value spaces represented in decimal format with leading and trailing whitespaces allowed and trimmed.
• Decimal point is not allowed.
• "nonNegativeInteger", "positiveInteger", "nonPositiveInteger" and "negativeInteger" lexical representations after whitespaces are trimmed should match this regular expression: /[\-+]?[0-9]+/.

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
-->
<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
-->
<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:

```c:\Progra~1\Java\jdk1.7.0_07\bin\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
```

Last update: 2013.