W3C home > Mailing lists > Public > xmlschema-dev@w3.org > May 2003

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

From: Jeni Tennison <jeni@jenitennison.com>
Date: Tue, 27 May 2003 17:18:50 +0100
Message-ID: <170431525270.20030527171850@jenitennison.com>
To: "Shital Joshi" <shital@mfgsys.com>
CC: xmlschema-dev@w3.org

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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:37 GMT