How does one decide when to use an attribute or an element for representing data?

Thijs Stalenhoef

(From Thijs Stalenhoef) Although technically the above two orders are the same there are a couple of things you might want to consider.
Elements can contain other elements, attributes do not have this kind of functionality (i.e. their content has no explicit structure). So if you want to replace for instance content between <UNIT> and </UNIT> with something more complex then "PC" (for instance something containing further XML tags!) you'll run into trouble with the first implementation. Ordering is another thing.
Attributes may only appear once in an element and their order may be completely random. Elements however must always appear in the same order and can occur as often as you like (or as your schema/DTD says they can occur).

Ways to think about attributes are as data that is of lesser importance, meta-data or properties. You are free to implement elements and attributes as you see fit. There are no real rules. Just keep the above in mind when setting up an XML document.

(From Benoit Xhenseval) One "standard" approach is to use elements to represent data fields and attributes to represent metadata and key.

The rationale is that:
  • elements are that elements make your design easier to extend than attributes
  • It is difficult to model complex structures using attributes
  • the attribute support is not uniform amongst parsers
  • elements can be enforced by a DTD

So, in you example, I would definitely go for your second option.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact