Page 1 of 1

xs:nil and xs:string

Posted: Fri Jul 07, 2006 9:33 am
by zasoxygen
Hello

can anybody explain me why xsi:nil is necessary for an empty element when the type is date, but when the type is string it works without.


The first XML is valid against the following schema

<el xsi:noNamespaceSchemaLocation="schema.xsd">
<element_1/>
</el>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="a">
<xs:sequence>
<xs:element name="element_1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="el" type="a"/>
</xs:schema>


When I change the type from string to date the XML is not valid anymore.
To get it valid again the schema and the XML must be changed.

<el xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
<element_1 xsi:nil="true"/>
</el>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="a">
<xs:sequence>
<xs:element name="element_1" type="xs:date" nillable="true"/>
</xs:sequence>
</xs:complexType>
<xs:element name="el" type="a"/>
</xs:schema>


Thanks

Stephan

Posted: Fri Jul 07, 2006 9:52 am
by george
Hi Stephan,

In the fist case you have an empty string as value for your element and that is a valid xs:string instance. An empty string on the other hand is not a valid xs:date instance.

Best Regards,
George

Posted: Fri Jul 07, 2006 10:06 am
by sorin_ristache
Hello,

The xs:string type includes all the string values allowed by the xs:string definition of the XML Schema specification and this includes the empty string but the xs:date type does not include such an empty value. That means an xs:date element can contain no text node child only if that element is defined as nillable in the XML Schema of that XML document.

Regards,
Sorin