What are the guidelines for using elements vs. attributes?

What are the guidelines for using elements vs. attributes?

The following guidelines are extracted or paraphrased from the Draft Federal XML Developer's Guide (April 2002).

Use elements that extend easily for maximum flexibility, and use attributes to reduce complexities or when forced to maintain simple content.

One of the key schema design decisions is whether to represent an information element as an XML element or attribute.  Once an information item has been made an attribute, it cannot be extended further.  There cannot be multiple uses of it within the same element (i.e. 0 or 1, but no more).  Also, if enumerated, an attribute cannot be extended to add values to its enumeration list.

Unfortunately, some of these guidelines can be very subjective.  For example, what constitutes metadata and what constitutes data?  Often this depends upon the user perspective and the application.  For a model with very large applicability and a requirement for maximum flexibility, it is probably safer to use elements that can be more easily extended.  Using attributes may prevent unnecessary complexities or when forced to maintain simple content.

Attributes SHOULD:

Attributes SHOULD NOT contain long string values.

For these reasons and to promote uniformity, Federal guidelines discourage the use of attributes.  But if you must use them, keep them simple.