views:

66

answers:

5

I know the elements versus attributes debate has come up many times here and elsewhere (e.g. here, here, here, here, and here) but I haven't seen much discussion of elements versus attributes for simple property values.

So which of the following approaches do you think is better for storing a simple value?

A: Value in Element Content:

<TotalCount>553</TotalCount>
<CelsiusTemperature>23.5</CelsiusTemperature>
<SingleDayPeriod>2010-05-29</SingleDayPeriod>
<ZipCodeLocation>12203</ZipCodeLocation>

or B: Value in Attribute:

<TotalCount value="553"/>
<CelsiusTemperature value="23.5"/>
<SingleDayPeriod day="2010-05-29"/>
<ZipCodeLocation code="12203"/>

I suspect that putting the value in the element content (A) might look a little more familiar to most folks (though I'm not sure about that).

Putting the value in an attribute (B) might use less characters, but that depends on the length of the element and attribute names.

Putting the value in an attribute (B) might be more extensible, because you could potentially include all sorts of extra information as nested elements. Whereas, by putting the value inside the element content (A), you're restricting extensibility to adding more attributes. But then extensibility often isn't a concern for really simple properties - sometimes you know that you'll never need to add additional data.

Bottom line might be that it simply doesn't matter, but it would still be great to hear some thoughts and see some votes for the two options.

+1  A: 

With approach B, you have to remember the attribute name. An auto-completing XML editor can ease this, but still, with option A, using element values, there is one less thing to remember, and there is no ambiguity. For example, what does it mean if I define two attributes, like

<MyProperty code="ABC" date="01-02-03"/>

Which you choose, attributes or element value, will depend on your intended audience, how the XML files are produced, how they are validated and how often they are read (by people.)

mdma
Good point, thanks. Hadn't thought of that one.
MB
A: 

A: Value in Element Content is better. e.g.

<TotalCount>553</TotalCount>
<CelsiusTemperature>23.5</CelsiusTemperature>
<SingleDayPeriod>2010-05-29</SingleDayPeriod>
<ZipCodeLocation>12203</ZipCodeLocation>

(Answer for polling purposes only - I marked it community wiki so I don't get credit for upvotes.)

MB
A: 

B: Value in Attribute is better:

<TotalCount value="553"/>
<CelsiusTemperature value="23.5"/>
<SingleDayPeriod day="2010-05-29"/>
<ZipCodeLocation code="12203"/>

(Answer for polling purposes only - I marked it community wiki so I don't get credit for upvotes.)

MB
+1  A: 

I'm not sure this is worth a poll. Option B is obviously wrong. It uses redundant information, and will be difficult to validate using XML schema.

The general "debate" is about whether to use your option A or something like this:

<Measurements totalCount="553" 
              celsiusTemperature="23.5"
              singleDayPeriod="2010-05-29"
              zipcodeLocation="12203"/>
John Saunders
Hmm, yes, I see that B uses redundant information. Though I've validated attributes in XML Schema just fine using a <xs:simpleType> element nested inside the <xs:attribute>.
MB
A: 

What about:

553 23.5 2010-05-29 12203

SimpleXML shows the attributes only if You request it directly on the key print_r( $wheather->SingleDayPeriod ) not if You request oute structure print_r( $weather )

Schüssler