Validating XML (empty dateTimes & numerics)

C

craig.wagner

I have an element in my schema defined as follows:

<xs:element name="BillingDate" type="xs:dateTime" nillable="true"
minOccurs="0"/>

I use the schema to validate incoming documents using an
XmlValidatingReader in .NET 1.1.

If the document contains the above element with no data, for example:

<BillingDate></BillingDate>
<BillingDate/>

The validation throws an exception on this element telling me it
contains data that doesn't conform to the type. That kind of makes
sense, because the value in the above case is an empty string, and
that's not a valid date.

I can change the above tags to one of the following and then it passes
validation:

<BillingDate xsi:nil="true"></BillingDate>
<BillingDate xsi:nil="true"/>

I can also get rid of the element completely and it passes validation
(because minOccurs="0").

However, I would prefer to not have to go through every document
looking for empty tags and adding the xsi:nil="true" attribute or
removing them from the document.

The same thing happens with elements defined as numeric types (e.g.
xs:int).

Is there any way (other than the above identified solutions) to get the
validation process to pass these elements without an exception?
 
M

Martin Honnen

Is there any way (other than the above identified solutions) to get the
validation process to pass these elements without an exception?

If you try to define your own type as a union of say xs:dateTime and an
empty string then it should work. But if you use the predefined types
then you have to use xsi:nil="true" to allow an empty element.


<xs:element name="date" maxOccurs="unbounded">
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0" />
<xs:maxLength value="0" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:date" />
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top