« Getting the Fault Payload returned by BPEL Processes in RMI Clients | Main | Removing additional whitespaces in your Elements and Attributes »

Specifying null content in your XML Document using nillable and xsi:nil

At times, it is very important to distinguish between empty content and null content in your XML documents. XML Parsers consider text values of empty elements as an empty string, and not null.
For instance, <itemId></itemId> and <itemId/> are both nodes whose text content is an empty string. When an XML Parser
parsers the document containing this element, the parser treats both variants of the itemId element as one with empty string value.
If you wish to explicitly mark the element to have a value of null as opposed to an empty content, you should declare the element in the schema
to be "nillable", signalling that the value for the element can come in as null at runtime.


For instance <xsd:element name="itemId" nillable="true"/>



At runtime, the element has to be explicitly marked with the "xsi:nil="true" to indicate the null content.
For e.g.
<itemId xsi:nil="true"/> or <itemId xsi:nil="true"></itemId>. Any schema aware XML parser will then be able to interpret the element content to have a null value.


But you cannot have xsi:nil="true" for elements that have non-null content. For e.g.
<itemId xsi:nil="true">hello</itemId> is illegal.
But you are free to have attributes on elements that have xsi:nil="true".


Note that "xsi" is  just a prefix - the "nil" attribute comes from the XML Schema instance namespace - http://www.w3.org/2001/XMLSchema-instance. You could define any arbitrary namespace prefix to refer to the "nil" attribute.


Another important point to note is that you cannot define "xsi:nil" on attributes. They can be defined only on elements.
Moreover, it is also illegal to define nillable on element references."ref" and "nillable" are mutually exclusive attributes.
For e.g.


<element ref="ItemId" nillable="true"/> is invalid.


Nillable should be specified on the actual element definition.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About This Entry

This page contains a single entry from the blog posted on November 1, 2007 4:45 PM.

The previous post in this blog was Getting the Fault Payload returned by BPEL Processes in RMI Clients.

The next post in this blog is Removing additional whitespaces in your Elements and Attributes.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type and Oracle