Possible Duplicate:
Should I use Elements or Attributes in XML?
I have never been able to figure out when to use xml attributes. I always use elements. I just read this w3schools article. The article states that it is bad practice to use attributes because:
The only exception that it states is when you are assigning an id to a tag.
Is this correct? Why do attributes even exist then? Was it a design error with xml? Is there something I am missing here?
The only reason I could think of for using attributes would be for one to one relationships. ie: name. But it would have to be a one to one relationship to something that is a primitive (or string). Because it would be important that in the future you would not want to break it up into several differnt sections. ie:
<date> May 23, 2001 </date>
to:
<date>
<month> May </month>
<d> 23 </d>
<yr> 2001 </yr>
</date>
Because this would not be possible with an attribute.
Bonus question: In the date example would it be possible to do something like this:
<date>
<default> May 23, 200 </default>
<month> May </month>
<d> 23 </d>
<yr> 2001 </yr>
</date>
To give future applications more (or different) information while still offering existing apps the same format? Or would you have to do this:
<date> May 23, 2001 </date>
<NEWdate>
<month> May </month>
<d> 23 </d>
<yr> 2001 </yr>
</NEWdate>
Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements.
You can't. Attribute names are unique per element. If you need to have multiple bits of data under the same name, then the usual solutions are either a space separated list or child elements.
HTML element holds the content. HTML attributes are used to describe the characteristic of an HTML element in detail. Whatever written within a HTML tag are HTML elements. HTML attributes are found only in the starting tag.
There are three types of attributes described below: String types Attribute: This type of attribute takes any string literal as a value. Notation Type: This attribute is used to declares that an element will be referenced to a notation which is declared somewhere else in the XML document.
Attributes are good when you want to attach information to other information, perhaps to describe how the information should be interpreted. For example:
<speed unit="mph">65</speed>
The points you list about elements are correct, and I would add the following:
But sometimes using an element to model a data point is overkill -- particularly when you have a lot of small, heterogeneous data points within a single parent element. Using attributes for simple things can improve readability. Some will probably argue that XML isn't readable or meant to be read/edited by humans... but I do it all the time.
Consider this example (basic hyperlink):
<a href="http://www.htmlhelp.com/" title="Help Information" target="_top">Web Design Group</a>
Would you like it if you had to write or read it this way instead?
<a>
<href>http://www.htmlhelp.com/</href>
<title>Help Information</title>
<target>_top</target>
<text>Web Design Group</text>
</a>
To me that looks like a lot of noise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With