XML, what is this: null or empty element?

Paul L picture Paul L · Aug 30, 2011 · Viewed 48.2k times · Source

Regarding to my other question: XML deserialize null elements?

I've got elements like these from a third-party server for API testing:

<Taxable />
<DefaultPurchasePrice />

I just realized that now I am confusing myself about whether elements like that represent null object or empty.

Talking about objects, they are same, null object normally means empty object reference right? But trying to map a XML element to a datafield/value, they could be different i.e null string is empty string, but for Decimal price or Boolean value, they are undefined which equal to empty, but will not be null, unless they are defined as nullable.

Again, the problem with my XmlSerializer is that wouldn't handle transfer an empty element like that. could I easily fix that in my code. Or should I ask people provide the XML have a well defined XML? Because seems that an empty XML element like that is undefined: it is here, but could be either null or empty doesn't matter for XML element itself? But for that element my code need figure out how to deal with that, unless I set all my C# class datafield as string type. Otherwise, if my code trying to directly map an empty or null XML element to a certain datafield, it will fail for sure.

I have to ask this question because I encountered XML have lots of those elements and for those special elements, my .NET XMLserialization code need to mapped those field as string and if the string is not empty, I case them into correspondent datatype, otherwise I set them to null. And I end up remove those empty element before I do my deserialization because it is much easier. But I do wandering: "What am I really doing in my code? Did I just remove null elements or empty elements? Because they are clearly different! But people writing that XML think they are same, because XML itself don't have concept of 'null', and some people argue that it is my responsibility to decide whether it is null or empty. But XML do enable you to represent 'null' elements in a clearer way

Edit:

In the example I presented, those two elements clearly should be null rather than empty elements. XML don't really have a concept of null, but those elements could either be omitted (if they are null, don't put them into XML) or using better representation as mentioned be @svick. Or in other cases, empty elements should be used when they make sense. But not for Decimal or Boolean.

Answer

Merlyn Morgan-Graham picture Merlyn Morgan-Graham · Aug 31, 2011

<Taxable />
<DefaultPurchasePrice />

what is this: null or empty element?

It is empty. It is semantically the same as:

<Taxable></Taxable>
<DefaultPurchasePrice></DefaultPurchasePrice>

The only "null" concept that exists in XML itself is a missing element. You can't specify an element and say that it is null, using XML's semantics. You must have some sort of schema (an explicit schema, such as DTD, XSD, or Schematron, or an implicit/logical schema) to do this.

This does not restrict an application that interprets the XML, though, or official XML-related technologies. For example, XSD has an xsi:nil="true" attribute. Even though you can apply some of these schema attributes to the XML, those attributes aren't "pure" XML; They are an addition supplied by XSD schemas.

This is a case where XSD was free to build its own semantics on top of the basic XML structure, and add an explicit nil where one didn't exist. This flexibility is considered an advantage of XML.