Book HomeXML SchemaSearch this book

11.3. Defining Nil (Null) Values

This feature was introduced into W3C XML Schema to map the notion of "null" values, which is one of the core requirements of the relational model as defined by Codd. The W3C makes a subtle distinction between an empty element and an element that is null (or "nil," to use the name chosen for the xsi attribute that conveys the information). The two notions are considered different, but related (an empty element is not always null, but a null element must be empty).

The fact that an element can be nil is defined in the element definition and short-circuits all the content type definitions. It may be used for simple types, complex content, and simple content complex types; and it may be used even if the content model forbids empty contents. In this case, the "nillable" character of the element takes precedence over the content model, and empty contents will be accepted when marked as nil. Again, since the fact that a value is nil is expressed using an attribute in the instance document, the mechanism does not apply to attributes that cannot have a null value.

One last thing to note before we look at some examples is that attributes are not affected by the fact that an element is nil, and they are still allowed or required if defined in the element.

The usage of this feature is rather simple. For example, if we want to allow nil values for the author of a book, we just set the nillable attribute in the element definition (nillable attributes cannot be specified in an element reference):

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

This declaration lets us declare null values in the instance documents:

<author xsi:nil="true"

This is possible despite the fact that the author element had not been declared as allowing empty content.

NOTE: The difference between nil and empty values is very slim. Assuming the author element is defined as nillable and has a content model allowing empty contents, the difference between:

<author xsi:nil="true"



is only a xsi:nil flag in the PSVI.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.