TExcelTAD
Home › XML › XML Attributes

XML Attributes

Attributes are name–value pairs written inside a start tag. They usually carry extra information about an element rather than the element's main content.

xml
<photo id="p104" format="jpeg">Sunset over the bay</photo>

id and format are attributes of the photo element. Their values are always text.

Attribute rules

  • Values must be quoted — with "double" or 'single' quotes.
  • The same attribute may appear only once on an element.
  • Attribute names follow the same naming rules as element names.
  • Order doesn't matter: <a x="1" y="2"/> and <a y="2" x="1"/> mean the same thing.

If a value itself contains a double quote, wrap it in single quotes or use an entity:

xml · try it yourself
<quotes>
  <quote by='Grace "Amazing" Hopper'>It's easier to ask forgiveness than permission.</quote>
  <quote by="Grace &quot;Amazing&quot; Hopper">Same speaker, different quoting.</quote>
</quotes>

Duplicate attributes are an error:

xml · try it yourself
<photo id="p104" id="p105"/>

Elements or attributes?

The same information can be written either way:

xml · try it yourself
<people>
  <person gender="female" born="1906">Grace Hopper</person>
  <person>
    <gender>female</gender>
    <born>1906</born>
    <name>Grace Hopper</name>
  </person>
</people>

Both are fine XML, so which should you choose? There is no hard rule, but experienced designers use these guidelines:

Use an attribute when… Use a child element when…
The value is small and simple (an id, a code, a unit) The value may contain structure or several parts
It is metadata about the content (lang="en", unit="kg") It is the content
There is only ever one value There may be several values (<phone> twice)
The value may grow in future versions
The text could be long or contain markup

Attributes have real limitations: they can't hold several values, can't contain child elements, and are harder to extend later. For that reason many formats keep data in elements and metadata in attributes.

IDs and references

A common use of attributes is giving elements an identifier that other elements can point to:

xml · try it yourself
<library>
  <author id="a1">Ursula K. Le Guin</author>
  <author id="a2">Terry Pratchett</author>
  <book authorRef="a1">A Wizard of Earthsea</book>
  <book authorRef="a2">Small Gods</book>
  <book authorRef="a1">The Dispossessed</book>
</library>

This avoids repeating the author's name in every book. A DTD or schema can even enforce that every authorRef matches an existing id.

Note: Attribute values are normalised by the parser: tabs and line breaks inside a value become spaces. Don't rely on attributes for formatted, multi-line text — use an element.

Special attributes

A few attribute names starting with xml have defined meanings:

Attribute Meaning
xml:lang="fr" Language of the element's content
xml:space="preserve" Ask applications to keep white space as written
xml:id="x1" A document-wide unique identifier
xmlns, xmlns:prefix Namespace declarations (next lessons)

Test yourself

Which is a legal attribute?

  1. <car color=red>
  2. <car color="red" color="blue">
  3. <car color='red'>
  4. <car color>

When is a child element usually better than an attribute?

  1. For a short id
  2. When the value could repeat or contain structure
  3. For a unit like kg
  4. Never