Quotes

Friday, November 4, 2016

Allow empty element for decimal type in XSD

Using nillable="true" on the element declaration is half-way there;
you also need to add xsi:nil="true" on the empty element itself, in
the instance document. For example, if you have the declaration:

<xs:element name="foo" type="xs:decimal" nillable="true" />

then the following are valid:

<foo>12.5</foo>
<foo xsi:nil="true" />

but the following are invalid:

<foo>bar</foo>
<foo />

If you want to avoid using xsi:nil to mark such elements, then you
should 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>

If you then declared the <foo> element with:

<xs:element name="foo" type="decimal-or-empty" />

then the following would be valid:

<foo>12.5</foo>
<foo />

Also you can define the above as below as well...
<SimpleType>
   <Union>
     <simpletype>
       for decimal
     </simpletype?
     <SimpleType>
      for string with empty value
     </simpletype?
   </union>
</simpletype>

No comments:

Post a Comment