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

19-06-2013

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

Example:

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=""/>
  </xs:restriction>
</xs:simpleType>

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"/>
  </xs:restriction>
</xs:simpleType>

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:simpleType>
  <xs:union memberTypes="int-1999 emptyString"/>
  </xs:simpleType>
</xs:element>

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.

Menol

ILT

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: