XSD validation for integer – How to allow empty values for integer while limiting value range (ILT)


How to write XSD to validate an integer field so that only a range of values and empty tags are allowed.


I wanted to allow any value between -1 and 999 and also had to allow empty tags for my XML tag.

Use of  nillable=”true” is not what you want for this. 

This how to do this.

1 – Create a custom (extended) type from string to allow an empty tag (allow an empty string)

<xs:simpleType name="emptyString">
  <xs:restriction base="xs:string">
  <xs:enumeration value=""/>

2 – Create another extended type from integer to allow your value range

<xs:simpleType name="int-1999">
  <xs:restriction base="xs:int">
  <xs:minInclusive value="-1"/>
  <xs:maxInclusive value="999"/>

It’s better if you do above steps in your header section (at top) for clarity

3 – Now define your field and use both types using the “Union”

<xs:element name="preNotificationPeriod" nillable="true">
  <xs:union memberTypes="int-1999 emptyString"/>

Now your field (StatusValue in this example) will allow empty tags (i.e. <StatusValue/> or <StatusValue></StatusValue>) or any value between -1 and 999

It will alert if it find any other (invalid) data

NOTE: Don’t misunderstand the use of nillable=”true” in here. It has no relavance for this purpose.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: