Re: allow empty element for <xsd:decimal> type

Hi Shital,

> I have an XML document with an element which is defined of
> <xs:decimal> type in schema.
> My program gives me an error where the xml element is empty. It says
> invalid value for datatype "decimal".
> I tried "nillable=true" attribute while defining element in schema.
> But it doesn't work.

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

and the following invalid:

<foo>bar</foo>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Tuesday, 27 May 2003 12:19:03 UTC