XML technologies / XML Schema / References to schemas and schema data types in XML documents / Defining nil (null) values

(Excerpt from “XML Schema” by Eric van der Vlist)

This feature was introduced in W3C XML Schema to represent null values, a central requirement of Codd’s relational model. The W3C subtly distinguishes an empty element from a null-valued element. It uses “nil” rather than the database term “null”, especially confusing in German and unrelated to the number 0, for the xsi attribute conveying this information. The concepts differ but are related: an empty element is not always nil, but a nil element must be empty.

Whether an element may be nil is specified in its definition and overrides further content-type definitions. It applies to simple types and complex types with simple or complex content, even where the content model forbids empty content. Nillability then takes precedence, allowing empty content marked nil. Again, this does not apply to attributes, which cannot have a nil value, since a value’s being nil is itself expressed by an attribute.

Before examples, note that whether an element is nil does not affect its attributes: they remain allowed or required as defined.

Usage is simple. To allow nil values for a book’s author, set attribute nillable in the element definition; nillable attributes cannot be set on an element reference:

<xs:element name="author" nillable="true">
   <xs:complexType>
      <xs:sequence>
         <xs:element ref="name"/>
         <xs:element ref="born"/>
         <xs:element ref="dead" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute ref="id"/>
   </xs:complexType>
</xs:element>

This declaration allows nil values in instance documents:

<author xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

This works even though element author is declared not to allow empty content.

The distinction between empty and nil values is subtle. Assuming the author element is defined as nillable and permits empty content, the difference between

<author xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

and

<author/>

is only an xsi:nil flag in the PSVI.

   

 

 

 

back to top