Monday, 31 March 2008

Schema Validation (Decimal field that allows empty string)

I am now preparing some xsd files to validate the XML files generated from AS400. Inside the XML, some fields are either decimal or empty string, but the standard XML schema validation only provides decimal datatype that does not allow null.

Finally I find the solution here:
http://osdir.com/ml/text.xml.schema.devel/2003-05/msg00062.html

The solution is to define a new datatype that allows elements to either have a decimal value or have an empty string as their value, like this:


<xs:simpleType name="decimal-or-empty">
<xs:union memberTypes="xs:decimal empty-string" />
</xs:simpleType>

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