Groups | Blog | Home
all groups > dotnet xml > april 2004 >

dotnet xml : The age old debate of Attributes vs. Elements



6tc1 NO[at]SPAM qlink.queensu.ca
4/21/2004 1:46:15 PM
Hi all, I have to decide on an XML structure going forward. The
structure is going to house a large amount of data. In the past I've
always just used the philosophy of "when in doubt use elements" - but
I just read this article:
http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/dnexxml/html/xml02212000.asp

And there is some indication that you will gain performance benefits
from XML that uses attributes rather than elements - example:

<grandparent>
<parent>
<child>
<name>
tony
</name>
</child>
<name>
Sarah
</name>
</parent>
<name>
Tom
</name>
</grandparent>
OR
<grandparent name='Tom'>
<parent name='sarah'>
<child name='tony'>
</child>
</parent>
</grandparent>

Now obviously in the first case you leave it open if you want to have
a deeper structure to name - like firstName, lastName, middleName,
alias, etc.

But if we were to assume that the XML structure will never change (I
know that is unlikely in many scenarios) but if it were to remain the
same, would there likely be a decrease in parsing time when I use the
transform method of XSLTransform (the .NET class) - in addition, I am
passing XPath queries (through a XPathNodeIterator object).

Thanks,
Jiho Han
4/22/2004 11:23:22 AM
I think the choice between using attributes vs. elements should be based on
the semantics of the data rather than the performance aspect.

I remember reading an article that provided a guideline for using attributes
vs. elements and it was something like use attributes for metadata for the
element node and elements for the actual content.
I am probably bastardizing the originally elegantly put statement here but
you get the gist. The author also mentioned and I agree that the line
between what is metadata and data isn't all that clear always. So you
should use your judgement.

For your example, I guess something like a person's names or SSN may be a
good fit for attributes whereas, what kind of car they drive and where they
live clearly works better as elements.
Just my 2 cents!

Jiho Han

[quoted text, click to view]

Oleg Tkachenko [MVP]
4/22/2004 12:04:05 PM
[quoted text, click to view]

The same in the second one - what's wrong with firstName attribute?

[quoted text, click to view]

Well, querying attributes is probably faster than child nodes, but large
attribute collection isn't good idea either.
I don't think there is any substantial difference. Do it as you feel
better for you.

--
Oleg Tkachenko [XML MVP, XmlInsider]
AddThis Social Bookmark Button