Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices: XML attribute vs XML element - When should I use elements and when should I use attributes? [duplicate]

Tags:

xml

Which would be the correct format for this XML data, are they equivalent or are there trade offs between the two?

1.

<sitemap>
  <category name="Animals">
    <section title="Dogs">
      <page url="/pics/greatdane.jpg" title="Great Dane"/>
    </section>
  </category>
</sitemap>

2.

<sitemap>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Great Dane</title>
    <url>/pics/greatdane.jpg</url>    
  </page>
</sitemap>

I've implemented the first example with my style sheet and it seems to work fine, but I'm unsure what the correct form should be.

like image 462
Rob Avatar asked Dec 27 '08 22:12

Rob


People also ask

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 is the difference if any between XML elements and XML attributes?

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.

Why do you avoid XML attributes?

Why should we avoid XML attributes. Attributes cannot contain multiple values but child elements can have multiple values. Attributes cannot contain tree structure but child element can. Attributes are not easily expandable.


3 Answers

The issue of attributes vs elements has been around for the better part of a decade and there is no right answer. Instead consider the differences and from that you should be able to decide which to use:

  • There can be only one instance of an attribute although you can enforce this with elements using DTD or XML Schema;
  • Attributes are unordered. Elements are not;
  • Attributes lead to a more concise syntax if there are no children. Compare:

    <page name="Sitemap"/>

to:

<page>
  <name>Sitemap</name>
</page>

I know which one I prefer;

  • Not really relevant now since DTDs aren't used much (if at all) over XML Schema but I'll add it anyway: DTDs allow default values (implied) for attributes but no such mechanism for elements; and
  • Elements, being elementss, can have children and attributes of their own. Attributes obviously cannot.

So, from your example, your innermost <page> element has a URL attribute (although it's an image for some reason--perhaps a preview icon? If so the attribute name is misleading). A webpage only has one URL (generally) so that'd be a good example of something that could be an attribute.

If on the other hand you wanted to list the images on the page, there could obviously be more than one so you'd need elements for that.

But, in the end, most of the time there's no right or wrong answer and it's largely a question of style.

like image 122
cletus Avatar answered Oct 23 '22 23:10

cletus


The two examples are not equivalent, because they form different hierarchies. Is a sitemap a list of categories, like the first example? Or is it a list of pages like the second example?

The answer to that is orthogonal to the element vs attribute question.

On the Element vs Attribute question: Here is your second example transformed to an attribute approach:

<sitemap>
 <page    
  category='Animals'
  section='Dogs'
  title='Great Dane'
  url='/pics/greatdane.jpg'
  /> 
</sitemap>

The above and your second case are equivalent. One consideration for choosing one versus the other is based on whether you may modify the schema in the future. Adding an attribute to the url element as in the following example would likely be a backward compatable change. The semantically same modification would be impossible in the attribute approach, as you cannot attach an attribute to an attribute.

<sitemap>
 <page>    
  <category>Animals</category>
  <section>Dogs</section>    
  <title>Great Dane</title>    
  <url nofollow="true">/pics/greatdane.jpg</url>
 </page> 
</sitemap>
like image 25
Steve Steiner Avatar answered Oct 23 '22 23:10

Steve Steiner


I think that the answer is quite obvious when you think about how you want to add more dogs:

<sitemap>
  <category name="Animals">
    <section title="Dogs">
      <page url="/pics/greatdane.jpg" title="Great Dane"/>
      <page url="/pics/wienerdog.jpg" title="Wiener Dog"/>
    </section>
  </category>
</sitemap>

or

<sitemap>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Great Dane</title>
    <url>/pics/greatdane.jpg</url>    
  </page>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Wiener Dog</title>
    <url>/pics/wienerdog.jpg</url>
  </page>
</sitemap>
like image 3
Svante Avatar answered Oct 23 '22 22:10

Svante