开发者

Difference and usage best practice of XML node value and XML node attribute

开发者 https://www.devze.com 2023-04-02 16:00 出处:网络
I see the following simpl开发者_JS百科e XML. <?xml version=\"1.0\"?> <customers> <customer ID=\"C001\">

I see the following simpl开发者_JS百科e 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消