Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard way to represent null attribute values in XML? [duplicate]

Tags:

xml

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.

like image 991
jglouie Avatar asked Nov 30 '12 15:11

jglouie


People also ask

How do you represent null in XML?

In an XML document, the usual way to represent a null value is to leave the element or attribute empty. Some business messages use a special value to represent a null value: <price>-999</price> . This style of null representation is supported by the DFDL and MRM parsers.

How are attribute values in XML represented?

An attribute element is used without any quotation and the attribute value is used in a single (' ') or double quotation (” “). An attribute name and its value should always appear in pair. An attribute value should not contain direct or indirect entity references to external entities.

How do you create an empty value in XML?

The most explicit way to represent a null value in XML is to include an empty tag set for the data item.

Can XML attributes be empty?

An element with no content is said to be empty. The two forms produce identical results in XML software (Readers, Parsers, Browsers). Empty elements can have attributes.


1 Answers

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?

like image 166
Tim Pietzcker Avatar answered Sep 25 '22 15:09

Tim Pietzcker