Elements and attributes are both containers for information. Many times the choice between an element and an attribute seems very arbitrary, almost matter of style. While the choice may indeed be arbitrary in some cases, the 'typical' roles of elements and attributes and the different types of content models and constraints these two containers support may tip the scales in one direction or the other.
One way to look at elements and attributes suggests that elements are the 'real' containers of data, while attributes annotate elements with additional information describing the content of the element. In the case of empty elements, attributes provide additional information about why the element is present and possibly about what content it represents. This approach has been used extensively in HTML and a variety of other document-oriented schemas, and works very well with style tools like Cascading Style Sheet, which make this structural assumption a key part of its display model.
Using elements to store content and restricting attributes to annotation does have a few drawbacks, however. Element markup is much more verbose than attribute markup, with start and end tags rather than a name and some quotes. Child elements can provide more flexibility, but that flexibility isn't always necessary. In some situations, like the exchange of large quantities of small chunks of information between databases, attributes may be more efficient containers.
The 'intrinsic' differences between elements and attributes in XML 1.0 tend to define the limits of what the two containers can be used for. The most fundamental difference, which will likely continue to hold through future iterations of XML development, is that elements can contain child elements as well as content, while attributes can only hold content. If it seems at all likely that you'll need to break down the information stored in a container, make that container an element.
Attributes do have some advantages over elements, however. In XML 1.0, and in the XML Schema working draft, only attributes may have default values assigned to them by the schema. In XML 1.0, attributes also have much stronger constraints - you can limit the acceptable values of an attribute to a class of values (notation or entity names) or provide an enumerated list of acceptable values. XML Schemas may change this, and XML Authority provides a larger list of possible constraints (data types) for elements than is available in XML 1.0.
Another field to explore before settling on whether to use elements or attributes is your processing software and parser. While element and attribute information is equally accessible in a tree-based interface (like those using the W3C's Document Object Model - DOM), developers working with stream-based interfaces (like the Simple API for XML - SAX) may have their own set of preferences for document structures.
Note: For more resources describing the issues involved in element and attribute usage, see Robin Cover's 'SGML/XML Elements versus Attributes' article.
Copyright 2000 Extensibility, Inc.
Suite 250, 200 Franklin Street, Chapel Hill, North Carolina 27516