Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Attributes and elements

 
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.
 
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...
 
Author and all-around good cowpoke
Posts: 13078
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
 
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
 
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.
 
Squanch that. And squanch this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic