Possible Duplicate:
Should I use Elements or Attributes in XML?
I have never been able to figure out when to use xml attributes. I always use elements. I just read this w3schools article. The article states that it is bad practice to use attributes because:
- attributes cannot contain multiple values (child elements can)
- attributes are not easily expandable (for future changes)
- attributes cannot describe structures (child elements can)
- attributes are more difficult to manipulate by program code
- attribute values are not easy to test against a DTD
The only exception that it states is when you are assigning an id to a tag.
Is this correct? Why do attributes even exist then? Was it a design error with xml? Is there something I am missing here?
The only reason I could think of for using attributes would be for one to one relationships. ie: name. But it would have to be a one to one relationship to something that is a primitive (or string). Because it would be important that in the future you would not want to break it up into several differnt sections. ie:
<date> May 23, 2001 </date>
to:
<date>
<month> May </month>
<d> 23 </d>
<yr> 2001 </yr>
</date>
Because this would not be possible with an attribute.
Bonus question: In the date example would it be possible to do something like this:
<date>
<default> May 23, 200 </default>
<month> May </month>
<d> 23 </d>
<yr> 2001 </yr>
</date>
To give future applications more (or different) information while still offering existing apps the same format? Or would you have to do this:
<date> May 23, 2001 </date>
<NEWdate>
<month> May </month>
<d> 23 </d>
<yr> 2001 </yr>
</NEWdate>