xs:nil and xs:string

This should cover W3C XML Schema, Relax NG and DTD related problems.
zasoxygen
Posts: 1
Joined: Thu Jul 06, 2006 4:20 pm

xs:nil and xs:string

Post by zasoxygen » Fri Jul 07, 2006 9:33 am

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

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Fri Jul 07, 2006 9:52 am

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

sorin_ristache
Posts: 4144
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache » Fri Jul 07, 2006 10:06 am

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

Post Reply