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:
<name>
or<age>
for this purposeAs Attributes:
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).