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.

3
  • 14
    How is this an exact duplicate? My question pertains to null-valued attributes, not null-valued elements? Commented Dec 1, 2012 at 14:57
  • 3
    This is in no way a duplicate. Attributes are not elements. Commented Jul 18, 2017 at 14:57
  • 2
    It's not duplicated. one is for attribute and the other is for element. Commented Apr 13, 2018 at 22:43

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks -- Is there an advantage or expected difference between <value /> and <value xsi:nil="true" />?
@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.
+1 I'd add you can define this in the schema by making the attribute "value" optional (optional="true" I believe).
We have a similar requirement and we used something like <value /> and <value xsi:nil="true" />, 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. stackoverflow.com/questions/774192/… provides some examples, too.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.