XML Attributes

Attributes attach extra facts about an element directly to its opening tag, as an alternative to nesting that data as a child element.

What Is an Attribute?

An attribute is a name/value pair written inside an element's opening tag, giving extra information about that element without adding a separate child. In <book category="fiction">, category is an attribute of <book>.

An Attribute in Action

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;book category="fiction"&gt;
  &lt;title&gt;Harry Potter&lt;/title&gt;
  &lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;/book&gt;
</pre>

</body>
</html>

The Same Data as a Child Element

Anything an attribute can express, a child element can express too — it's purely a design choice, not a technical requirement. The example above could just as validly be written with category as its own nested element instead of an attribute.

The Same Fact as a Child Element

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;book&gt;
  &lt;category&gt;fiction&lt;/category&gt;
  &lt;title&gt;Harry Potter&lt;/title&gt;
  &lt;author&gt;J K. Rowling&lt;/author&gt;
&lt;/book&gt;
</pre>

</body>
</html>

When to Prefer Attributes

There's no rule enforced by XML itself, but experienced authors follow a few conventions. Attributes work well for metadata about an element — an id, a unit, a date stamp, a language code — while the element's own content, and anything structured or repeatable, is usually better as a child element.

  • Use attributes for metadata that describes the element, not the core data itself, like an id or a unit.
  • Use child elements when the value might need to hold multiple pieces of data or repeat.
  • Attribute values cannot contain their own nested structure; child elements can.
  • An attribute name can only appear once per element, so an element can't have two id attributes.
  • Child elements are generally easier to extend later without breaking existing consumers of the data.

Attributes vs. Nested Structure

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;!-- Works fine: a single simple value as an attribute --&gt;
&lt;temperature unit="celsius"&gt;22&lt;/temperature&gt;

&lt;!-- Would NOT work well as an attribute: multiple related values --&gt;
&lt;address&gt;
  &lt;street&gt;221B Baker Street&lt;/street&gt;
  &lt;city&gt;London&lt;/city&gt;
&lt;/address&gt;
</pre>

</body>
</html>
Note: A single element can carry many different attributes, but each attribute name may only appear once per element. <book id="1" id="2"> is invalid XML.
Note: A useful rule of thumb: if the data feels like part of the record itself, make it a child element; if it feels like a label or property attached to the record, make it an attribute.
SituationAttributeChild element
Storing a single, simple valueGood fitAlso works
Storing multiple related valuesPoor fitGood fit
Value may need to repeatNot possibleGood fit
Metadata about the element (id, unit, date)Good fitWorks, but more verbose

Exercise: XML Attributes

What must every XML attribute value have?