« September 28, 2007 | Main | November 14, 2007 »

November 1, 2007 Archives

November 1, 2007

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.

About November 2007

This page contains all entries posted to Ramkumar Menon's Blog in November 2007. They are listed from oldest to newest.

September 28, 2007 is the previous archive.

November 14, 2007 is the next archive.

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

Powered by
Movable Type and Oracle