XML: When to attribute and when to element?

When defining an XML document, when are attributes most appropriate, and when are elements best? This is something that I've generally decided based more on intuition than a good set of rules.

Recently, at work, the need has arisen to define quite a few XML message payloads. I've also had the good fortune to work with Erik Doernenburg and we had a chat about attributes vs. elements. Largely thanks to Erik, here are some guidelines that could come in handy when making such a decision.

An attribute is best used to represent:

  • an id
  • metadata (eg, like rel and class in HTML)
  • a value from a small, closed set of values which interpreting programs rely on (eg, values that end up as application constants)

If none of the above apply, an element would likely be the best choice.


Actions

Information