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.

1

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.