I have seen null
elements represented in several ways:
The element is present with xsi:nil="true"
:
<book>
<title>Beowulf</title>
<author xsi:nil="true"/>
</book>
The element is present, but represented as an empty element (which I believe is wrong since 'empty' and null
are semantically different):
<book>
<title>Beowulf</title>
<author/>
</book>
<!-- or: -->
<book>
<title>Beowulf</title>
<author></author>
</book>
The element is not present at all in the returned markup:
<book>
<title>Beowulf</title>
</book>
The element has a <null/>
child element (from TStamper below):
<book>
<title>Beowulf</title>
<author><null/></author>
</book>
Is there a correct, or canonical way to represent such a null
value? Are there additional ways than the above examples?
The XML for the examples above is contrived, so don't read too far into it. :)
Empty XML Elements An element with no content is said to be empty. In XML, you can indicate an empty element like this: <element></element> You can also use a so called self-closing tag: <element />
XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these. Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.
XML elements must follow these naming rules: Element names are case-sensitive. Element names must start with a letter or underscore. <1Slightbook> is not an XML element. An XML element can contain several things such as text, attributes, and a mix of both elements.
Outgoing XML attributes mapped to fields If a field is null, AR System generates an attribute with empty content. If a character field contains an empty string, AR System generates an attribute with empty content.
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:
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.
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]
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>
In many cases the purpose of a Null value is to serve for a data value that was not present in a previous version of your application.
So say you have an xml file from your application "ReportMaster" version 1.
Now in ReportMaster version 2 a some more attributes have been added that may or not be defined.
If you use the 'no tag means null' representation you get automatic backward compatibility for reading your ReportMaster 1 xml file.
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
.)
Simply omitting the attribute or element works well in less formal data.
If you need more sophisticated information, the GML schemas add the attribute nilReason, eg: in GeoSciML:
xsi:nil
with a value of "true" is used to indicate that no value is availablenilReason
may be used to record additional information for missing values; this may be one of the standard GML reasons (missing, inapplicable, withheld, unknown
), or text prepended by other:
, or may be a URI link to a more detailed explanation.When you are exchanging data, the role for which XML is commonly used, data sent to one recipient or for a given purpose may have content obscured that would be available to someone else who paid or had different authentication. Knowing the reason why content was missing can be very important.
Scientists also are concerned with why information is missing. For example, if it was dropped for quality reasons, they may want to see the original bad data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With