11

Possible Duplicate:
What is the correct way to represent null XML elements?

Is there a standard way to represent null attribute values in XML?

We have an existing XML format that expects fragments similar to:

<items>
  <item key="key_goes_here" value="value_goes_here" />
  ...
</items>

The original format didn't anticipate the need to distinguish between a null value and an empty string value -- however, that is important (now).

My gut says to create a new format that avoids attributes for nullable values, and use elements for them instead:

<items>
  <item key="key_goes_here_and_is_never_null">
    <value xsi:nil="true" /> 
  </item>
</items>

That said, I'd rather keep attributes if there's a standard way to represent null attribute values in XML.

Community
  • 1
  • 1
jglouie
  • 12,523
  • 6
  • 48
  • 65

1 Answers1

4

I don't know about any standard, but how about using

<item key="key" />

for items that don't have a value,

<item key="key">
    <value /> 
</item>

for items that have an empty string as a value and

<item key="key">
    <value>This is the value</value> 
</item>

for items that actually have a value?

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    Thanks -- Is there an advantage or expected difference between and ? – jglouie Nov 30 '12 at 15:20
  • @LemonBeagle: To be honest, I've never seen the latter before, but I'm not much of an XML expert. It seems to me a simple empty tag conveys the message just fine and is easy to understand. – Tim Pietzcker Nov 30 '12 at 15:22
  • 1
    +1 I'd add you can define this in the schema by making the attribute "value" optional (`optional="true"` I believe). – wds Nov 30 '12 at 15:25
  • +1 -- @LemonBeagle Think of it this way: you want to represent that something is missing (nil == has no value) and the clearest way to do that is not giving any value. Additional syntax is misleading in my opinion. – David Lantos Nov 30 '12 at 15:32
  • 3
    We have a similar requirement and we used something like and , where the former represents empty strings and the latter represents null values. Also, be careful about using no tag as null because there is a difference between something being null to start off with versus being set to null. This is especially true when you're sending changes in as part of a request. http://stackoverflow.com/questions/774192/what-is-the-correct-way-to-represent-null-xml-elements provides some examples, too. – tjg184 Nov 30 '12 at 16:53
  • @tjg184: Good point, and thanks for linking this question. This absolutely makes sense. – Tim Pietzcker Nov 30 '12 at 18:51
  • 2
    @TimPietzcker Although this may have solved the OP's problem, this *does not* answer the question, as stated. Does anyone know how to represent null *attributes*? I do not have the power to change the receiving end, which expects attributes not elements. – Dan Bechard May 16 '16 at 12:50