1

I see the following simple XML.

<?xml version="1.0"?>
<customers>
   <customer ID="C001">
      <name>Acme Inc.</name>
      <phone>12345</phone>
   </customer>
   <customer ID="C002">
      <name>Star Wars Inc.</name>
      <phone>23456</phone>
   </customer>
</customers>

I am wondering why the ID is stored as an attribute instead of another internal node?

Is there any best pratice or conventions for this?

What's the difference between a node value and a node attribute?

Thanks.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482

1 Answers1

1

There are no hard-and-fast rules for this, and it is a matter of taste as much as anything. http://www.w3schools.com/dtd/dtd_el_vs_attr.asp has a nice overview. Note that there are quite a few drawbacks to using attributes so child elements are probably preferable in most cases. However, there is a strong convention for making ID an attribute (this is mentioned in the referenced text as well).

Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54