Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Attributes v's Elements  RSS feed

 
Richard Butterwood
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the downside of using attributes over extra elements. Several books state that it is purely down to the designer's personal preference on using attributes or elements.

Personally I think that attributes are nicer as it doesn't bloat the xml.

Here are two examples with and without attributes. Which is best?

With Attributes
<RequestBatch ConsumerKey="..." Password="..." Language="..." DateTime="..." SerialID="...">
<Request RequestID="..." Company="..." SerialID="...">
...
</Request>
</RequestBatch>

Without Attributes
<RequestBatch>
<Authentication>
<ConsumerKey>...</ConsumerKey>
<Password>...</Password>
<Language>...</Language>
<DateTime>...</DateTime>
</Authentication>
<Request>
<RequestHeader>
<RequestID>...</RequestID>
<Company>...</Company>
<SerialID>...</SerialID>
</RequestHeader>
...
</Request>
</RequestBatch>
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guideline is if something feels like a "header" or like "meta-information", it goes into an attribute, if it feels like the actual information, it goes into an element. Totally subjective, obviously, and a very flexible rule

There are technical considerations as well, of course - some of the things that can go into a CDATA section can't be put into an attribute. And elements have an ordering, while attributes don't.

Since you're asking in the WS forum, this thread might be of interest to you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!