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 xsi:nil="true" />

but the following are invalid:

<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 name="empty-string">
  <xs:restriction base="xs:string">
    <xs:enumeration value="" />

If you then declared the <foo> element with:

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

then the following would be valid:

<foo />

and the following invalid:




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

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:11 UTC