When is an attribute an attribute?

Jim Amsden jamsden at us.ibm.com
Tue Apr 7 02:48:36 BST 1998

I think it's best to treat this as an object modeling problem first, and then
an XML representation. The distinction between attribute and content element
then becomes the distinction between an attribute and a containment
relationship with another object. Object attributes are atomic, referentially
transparant characteristics of an object that have no identity of their own.
Generally this corresponds to primitive data types, but this can be somewhat
arbitrary too (e.g., Strings, Date, etc.). Taking a more logical view, an
attribute names some characteristic of an object that models part of its
internal state, and is not considered an object in its own right. That is, no
other objects have relationships to an attribute of an object, but rather to
the object itself.

So if the thing you want to capture has internal structure of its own, or can
be referenced through a link, or can be contained in more than one element,
then its an element, otherwise it's probably an attribute. Note that attributes
have a numer of advantages over content elements:

1. they can have names that indicate the role the value plays in the element.
Element contents have content names, but there is no way to say what role the
content plays in any particular element that contains it.

2. attributes can have default values.

3. attributes have (minimal) data types

4. attributes take up less space as there is no need for an end tag

5. attributes are easier to access in DOM.

There are also some disadvantages:

1. attributes aren't as convenient for large values, or binary entities.

2. values containing quotes can be a bother.

3. attributes can't contain other elements. This isn't really a disadvantage,
but part of what it means to be atomic.

4. white space can't be ignored in an attribute.

My recommendation is to use attributes unless you can't, and certainly use them
to avoid mixed data content in elements whenever possible. The idea is to
encapsulate as much as you can in an individual object but not too much. Use
the principles of data normalization, they work fine here too.

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)

More information about the Xml-dev mailing list