Forum Moderators: open

Message Too Old, No Replies

XML: Tags vs Attributes

Just wondering...

         

MakTheYak

2:00 pm on Feb 23, 2006 (gmt 0)

10+ Year Member



Hi, I was just setting about making a Flash photo album with XML feed to get the picture data into it and I can't think of any reason why something like this:


<photo>
<src>gedowar_clean.jpg</src>
<title>The upcoming Ghibli movie</title>
<sound>song1.mp3</sound>
<transition>fade</transition>
</photo>

Should be any better than somthing like this:


<photo src="gedowar_clean.jpg" title="The upcoming Ghibli movie" sound="song1.mp3" transition="fade" />

apart from the obvious readability advantage of the first, and the size advantage of the second.

Are there any guidelines which I should be applying or any logic which makes the descision easier?

Cheers!

choster

5:47 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



XML does not have "tags," though it does have elements :).

Somewhat more helpfully, attributes provide information about elements, but the definitions are pretty fluid and what info to store in what node more or less depends on your philosophy.

Generally speaking, however, you can do more with element nodes. For instance, you can't expand an attribute to contain more information in the future, whereas you can create child elements to do so. You also can't have multiple attributes on an element.

In terms of processing, attributes have no order or structure, so you can't, for instance, write code to select the first attribute on an element like you can to select the first child element. IMHO the XPath is also more readable, especially in complex documents.

Whether one or the other offers superior performance measures, I can't say. So it is really up to you.

choster

8:42 pm on Feb 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also can't have multiple attributes on an element.
Now that I read that, I see how that could be confusing. What I mean is that you can't have multiples of the same attribute on an element, like

<city nickname="L.A." nickname="City of Angels" />

But

<city>
<nickname>L.A.</nickname>
<nickname>City of Angels</nickname>
</city>
is perfectly fine.

MakTheYak

4:24 pm on Feb 25, 2006 (gmt 0)

10+ Year Member



Thanks, that helps a lot. It sounds like it's a lot better in general and makes more sense to do it all with elements. Option 1 it is then!