469,603 Members | 2,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

Validating XML with empty dateTime & numeric elements

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?

Feb 8 '06 #1
2 8321
Craig,

The following should do what you need. This example uses a union of
dateTime type and an empty string type.

<xs:element name="BillingDate" type="nillableDate" minOccurs="0"/>

<xs:simpleType name="nillableDate">

<xs:union memberTypes="xs:dateTime emptyString"/>

</xs:simpleType>

<xs:simpleType name="emptyString">

<xs:restriction base="xs:string">

<xs:length value="0"/>

</xs:restriction>

</xs:simpleType>
--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

<cr**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Feb 8 '06 #2
That worked perfectly. Thanks.

Feb 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by David Graham | last post: by
30 posts views Thread by Toni Mcintyre | last post: by
8 posts views Thread by Davey | last post: by
4 posts views Thread by Eric | last post: by
5 posts views Thread by anony | last post: by
2 posts views Thread by craig.wagner | last post: by
6 posts views Thread by Richard | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.