When to use attributes as opposed to elements


Re:         When to use attributes as opposed to elements 
Author:     G. Ken Holman
Email:      gkholman@CraneSoftwrights.com
Date:       1999/01/11
Forums:     comp.text.xml 


On Thu, 7 Jan 1999 15:58:56 +1100, "Phill Edwards"
<phill_edwards@hotmail.com> wrote:

>I must apologise in advance if I use incorrect terminology, but I'm trying
>to understand when to define a piece of information as an attribute of an
>element in ATTLIST rather than as an element.
>...
>On what basis do you decide which is the best way to go?

This is more often a religious question rather than a technical
question.

Lexically, there is little difference between the two, so other
criteria should be used to decide when a value is "best" coded as an
element or as an attribute.

The common reference to discussions of this nature is:

  http://www.oasis-open.org/cover/elementsAndAttrs.html

Personally, I follow my own guidelines that have evolved over the
years.  These are *never* hard and fast rules, and there are often
exceptions to meet specific situations ... but the first questions I
ask myself (not always in this order) are:

[1] Does the value have one of an enumeration of values or is the
value free-form?

   [1a] - enumerated values can be name token groups in attributes
(see XML productions [57] and [59])
        - SGML (not XML) also has an attribute type of NUMBER
   [1e] - no restrictions on values for the content of elements

[2] Is the value to be specified, manipulated, organized, consumed by
a program or by a human?

   [2a] - I use attributes for computer-manipulated values
   [2e] - I use elements for homan-manipulated values

[3] Does the information represent information *about* content, or is
the information the content itself?

   [3a] - I typically put meta-data in attributes
   [3e] - I typically put content into elements

[4] Is the information flat or hierarchical?

   [4a] - attributes are flat and a value has no hierarchy
   [4e] - elements can be either flat or hierarchical

[5] Is the information unordered or ordered?

   [5a] - multiple attribute values in a single start element have no
prescribed order
   [5e] - multiple child elements of an element can be modeled in a
prescribed order

[6] Is the content to be spell-checked?

   [6a] - I put values guaranteed to fail a spell-checker (or
undesired to be spell-checked) in attributes
   [6e] - I put values to be spell checked in elements

I hope this helps.

.......... Ken


G. Ken Holman                    gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.  http://www.CraneSoftwrights.com/xs/ea/
Training:   http://www.CraneSoftwrights.com/xs/ea/schedule.htm
Resources: http://www.CraneSoftwrights.com/xs/ea/resources.htm
Shareware: http://www.CraneSoftwrights.com/xs/ea/shareware.htm