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.
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.
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 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.
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:
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;
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.
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>
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>
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