What is the correct way to represent null XML elements?
Solution 1:
xsi:nil is the correct way to represent a value such that: When the DOM Level 2 call getElementValue() is issued, the NULL value is returned. xsi:nil is also used to indicate a valid element with no content even if that elements content type normally doesn't allow empty elements.
If an empty tag is used, getElementValue() returns the empty string ("") If the tag is omitted, then no author tag is even present. This may be semantically different than setting it to 'nil' (Ex. Setting "Series" to nil may be that the book belongs to no series, while omitting series could mean that series is an inapplicable element to the current element.)
From: The W3C
XML Schema: Structures introduces a mechanism for signaling that an element should be accepted as ·valid· when it has no content despite a content type which does not require or even necessarily allow empty content. An element may be ·valid· without content if it has the attribute xsi:nil with the value true. An element so labeled must be empty, but can carry attributes if permitted by the corresponding complex type.
A clarification:
If you have a book xml element and one of the child elements is book:series you have several options when filling it out:
- Removing the element entirely - This can be done when you wish to indicate that series does not apply to this book or that book is not part of a series. In this case xsl transforms (or other event based processors) that have a template that matches book:series will never be called. For example, if your xsl turns the book element into table row (xhtml:tr) you may get the incorrect number of table cells (xhtml:td) using this method.
- Leaving the element empty - This could indicate that the series is "", or is unknown, or that the book is not part of a series. Any xsl transform (or other evernt based parser) that matches book:series will be called. The value of current() will be "". You will get the same number of xhtml:td tags using this method as with the next described one.
- Using xsi:nil="true" - This signifies that the book:series element is NULL, not just empty. Your xsl transform (or other event based parser) that have a template matching book:series will be called. The value of current() will be empty (not empty string). The main difference between this method and (2) is that the schema type of book:series does not need to allow the empty string ("") as a valid value. This makes no real sense for a series element, but for a language element that is defined as an enumerated type in the schema, xsi:nil="true" allows the element to have no data. Another example would be elements of type decimal. If you want them to be empty you can union an enumerated string that only allows "" and a decimal, or use a decimal that is nillable.
Solution 2:
There is no canonical answer, since XML fundamentally has no null concept.
But I assume you want Xml/Object mapping (since object graphs have nulls); so the answer for you is "whatever your tool uses". If you write handling, that means whatever you prefer. For tools that use XML Schema, xsi:nil
is the way to go. For most mappers, omitting matching element/attribute is the way to do it.
Solution 3:
It depends on how you validate your XML. If you use XML Schema validation, the correct way of representing null
values is with the xsi:nil
attribute.
[Source]
Solution 4:
The documentation in the w3 link:
http://www.w3.org/TR/REC-xml/#sec-starttags
says that these are the recommended forms:
<test></test>
<test/>
The attribute mentioned in the other answer is a validation mechanism and not a representation of state. Please refer to: http://www.w3.org/TR/xmlschema-1/#xsi_nil
XML Schema: Structures introduces a mechanism for signaling that an element should be accepted as ·valid· when it has no content despite a content type which does not require or even necessarily allow empty content. An element may be ·valid· without content if it has the attribute xsi:nil with the value true. An element so labeled must be empty, but can carry attributes if permitted by the corresponding complex type.
To clarify this answer:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<!--This element should alway be empty-->
<BuildAttributes HardCover="true" Glued="true" xsi:nil="true"/>
<Index></Index>
<pages>
<page pageNumber="1">Content</page>
</pages>
<!--Valid representation of a null or empty ISBN-->
<ISBN></ISBN>
</Book>
<Book>
<!--Invalid construct since the element attribute xsi:nil="true" signal that the element must be empty-->
<BuildAttributes HardCover="true" Glued="true" xsi:nil="true">
<anotherAttribute name="Color">Blue</anotherAttribute>
</BuildAttributes>
<Index></Index>
<pages>
<page pageNumber="1">Content</page>
</pages>
<!--Missing ISBN could be confusing and misguiding since its not present-->
</Book>
</Books>
Solution 5:
You use xsi:nil
when your schema semantics indicate that an element has a default value, and that the default value should be used if the element isn't present. I have to assume that there are smart people to whom the preceding sentence is not a self-evidently terrible idea, but it sounds like nine kinds of bad to me. Every XML format I've ever worked with represents null values by omitting the element. (Or attribute, and good luck marking an attribute with xsi:nil
.)