Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attributes and elements

 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends ,
I have the following doubt:

<notes>
<note date="23/09/07">
<to>jane</to>
<from>Anne</from>
</note>
</notes>

in the above note element I've date as an attribute.
But date can also be defined as a child element like <to>and <from> of note

What difference does it make having date declared as a child element or as an attribute of note element.

cheers ,
Poonam K.
 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<notes>
<note date="23/09/07">
<to>jane</to>
<from>Anne</from>
</note>
</notes>

in the above note element I've date as an attribute.
But date can also be defined as a child element like <to>and <from> of note

What difference does it make having date declared as a child element or as an attribute of note element.

Then date would be an attribute for note and would be child for notes
It can be possible if you define your DTD/Schema accordingly...
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What difference does it make having date declared as a child element or as an attribute of note element.


To some extent this is a author style thing with no hard and fast rule. I have seen plenty of XML extremes with no attributes or all attributes. As you gain more experience with the various XML toolkits I'm sure you will develop your own style.

Mainly you should be aware of the limitations on the character content in attributes before you try to use nothing but attributes.

Bill
 
Dhanasekar Thangavelu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find the below limitations of using the attributes rather elements

attributes cannot contain multiple values (child elements can)
attributes are not easily expandable (for future changes)
attributes cannot describe structures (child elements can)
attributes are more difficult to manipulate by program code
attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document


-Dhanasekar.T
 
Manish Shah
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding as a node is more preferable as it is easily readable.
You can consider putting as an attribute if that attribute is short & constant.
What if in the future there is a change expected in date to specify date format information ..?
In this case you cannot go on adding attributes.
It is rather better to have date as node so that it can be open for having child attributes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic