OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help



   Re: [xml-dev] attributes vs. elements

[ Lists Home | Date Index | Thread Index ]

On Tue, Sep 10, 2002 at 01:21:37PM -0400, Maciejewski, Thomas wrote:
> This is sort of a newbie question but one that I never fully understood or
> maybe just don't know how to explain it to others. 
> When should one use an element and when should one use an attribute?

That's a very common and very important question, and it's probably in
a FAQ somewhere, but I have no idea which FAQ. In any case, there is no
hard and fast answer to that question.

The traditional distinction between elements and attributes is that the
former are for content (information of interest to the end user or the
consuming application) and the latter for metadata (information about
information), and there is no clearcut, universal distinction between
those two. Generally speaking, the text in a paragraph or the data in a
database record would be considered content; the date that content was
last modified will probably be metadata, but it's not hard to imagine
scenarios where you might want to treat it as content. Ultimately,
deciding whether something is content or metadata is up to the creators
and users of the information.

But back to elements and attributes. Some considerations that may affect
your choice:

  * Elements may not have more than one attribute with the same name, 
    but child element names can be repeated indefinitely (unless 
    forbidden by a schema, of course).

  * XML parsers must preserve the order of elements, but not attributes.

  [ given the above two statements, we can say that in programming
    terms, the attributes of any given element are analogous to a 
    hash (AKA mapping, dictionary, etc.), while the child elements 
    are analogous to an array ]

  * Attributes can't contain elements--though of course they can
    reference them.

  * White space in elements can be preserved, whereas white space in
    attributes is normalized (leading and trailing spaces are stripped,
    each extent of internal white space is collapsed to a single space

  * Attributes are more compact, since their names only occur once per
    instance and they have fewer 'overhead' characters: ="" vs. <> </>.

  * Many people find elements more readable.

I think there is a tendency for document-oriented applications to prefer
elements, and messaging and automated data-exchange applications to
prefer attributes--the rationale being that bandwidth is more important
and human-readability less so. However, in cases where human-readability
is unimportant, you might want to ask whether XML is an appropriate
solution at all.

That's my $.02.

Matt Gushee
Englewood, Colorado, USA


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS