|
Elements and attributes
are both containers for information. Many times the choice between
an element and an attribute may seem arbitrary, almost a matter of
style. But in making the decision, the 'typical' role of the data
item is considered and will usually tip the scale 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. The
decision really comes down to a set of questions that are asked to
determine whether we're dealing with an element or an attribute.
Questions including:
- Will the item need
to have sub-items? If so, only elements can have child elements
nested within them while attributes can not.
- Will the item be a common data
requirement of all users, that is, "core" information?
If so, elements usually represent the natural, core content
which would appear in every instance document while attributes
represent data of secondary importance.
- Will the item
describe data or the characteristics of data? Attributes
typically are used to describe the characteristics, or
properties, of an item.
- Does the data need to be
ordered.
- What software will
be processing and parsing the data? 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 prefer otherwise.
The committee recommends these
guidelines when defining data items:
- An Element should
be used to represent a piece of information that can be
considered an independent item.
- An Element should
be used when the information is related via a parent/child
relationship to another piece of information. In this case, the
element is also a sub-element of the element to which it is
related.
- An Attribute
should be used to represent any information "left
over" after defining the items that have relationships to
other items.
- An Attribute should be
used to represent any information that describes other
information, such as a status or ID.
- An Element must be used if an
item occurs multiple times, because attributes can have only one
occurrence in an element.
If it still not clear
whether an identifier should be an element or an attribute, it is
made an element. Elements are more extensible than attributes in an
evolving standard because elements can contain other elements or
substructures while attributes can not. If an item is defined as an
attribute initially, and then needs to be expanded to hold
fine-grained information, it must be changed to an element to be
modeled correctly.
|