[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: attributes vs. elements


attributes vs elements xml readability
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
    character).

  * 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
mgushee@h...
http://www.havenrock.com/

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.