XML Design – Choosing Between Attributes and Sub Nodes

xml

We want to export some data from our data base as XML. For example, a Person can have age, name and some other properties.

We have two choices to define the XML format.

Choice #1:

<Persons>
   <Person>
       <Age>16</Age>
       <Name>Richard</Name>
   </Person>
   <Person>
       <Age>34</Age>
       <Name>Eric</Name>
   </Person>
   ...
</Persons>

Choice #2:

<Persons>
   <Person Age="16" Name="Richard"/>
   <Person Age="34" Name="Eric"/>
   ...
</Persons>

So what's the difference between definition of sub node or attribute?
And what's the benefit of each choice?

Best Answer

There is no clear documentation/best practice for this, but, consider the alternatives, as you have:

As Element text:

  • it can be easier to display the data as xhtml, etc, where the text content is considered text, rather than markup or meta-data.
  • there can be more than one. If you need child content with multiple age or name rows, attributes won't allow this
  • if you need row level meta data, you have the option of using the attributes of <name> or <age> for this purpose

As Attributes:

  • the XML is more compact
  • XSLT and DocTypes are simpler to specify
  • you don't have to worry about whitespace (padding, indenting, line breaks), or other items that can be introduced (comments, PI's) in PCDATA areas (element text)
  • there can be only one! you don't have to worry about child content containing multiple age attributes.

I have spent a lot of time working with XML, and, in my opinion, for pure data communication, attributes should be used whenever possible. If the XML is likely to be used for presentation (XSLT, xhtml, etc.) then it may be better as text content (but not necessarily).

Related Topic