Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Elements vs. Attributes [duplicate]

Tags:

xml

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:

  • 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 DTD

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>
like image 261
sixtyfootersdude Avatar asked Jan 20 '10 17:01

sixtyfootersdude


People also ask

What is the difference between elements and attributes in XML?

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.

Can XML elements have multiple attributes with same name?

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.

When should you use an attribute versus an element?

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.

What are different types of elements and attributes in XML?

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.


2 Answers

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>
like image 146
Guffa Avatar answered Sep 22 '22 01:09

Guffa


The points you list about elements are correct, and I would add the following:

  • elements generally make prettier (more readable) diffs when you need to compare revisions of a file

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.

like image 26
Drew Wills Avatar answered Sep 24 '22 01:09

Drew Wills