Skip to content

XML Attributes

XML elements can have attributes, which provide additional information about the element.

Attributes are defined within the start tag of an element and consist of a name-value pair.

Attributes are specified directly inside the opening tag of an element:

<file type="image/gif" size_kb="15">
computer.gif
</file>
<link rel="stylesheet" href="styles.css"/>

In the first example, type and size_kb are attributes of the file element. In the second, rel and href are attributes of the empty link element.

Attributes often provide metadata (data about the element’s content) or configuration details, rather than the primary content itself.

Unlike some older HTML practices, attribute values in XML must always be enclosed in quotes.

You can use either single quotes (') or double quotes ("). Choose one and be consistent.

Using double quotes:

<person gender="female">

Using single quotes:

<person gender='female'>

If the attribute value itself needs to contain the quote character used for enclosing, you must either use the other type of quote or use character entities:

Using alternate quotes:

<gangster name='George "Shotgun" Ziegler'>

Using character entities (&quot; for ", &apos; for '):

<gangster name="George &quot;Shotgun&quot; Ziegler">

Attributes vs. Child Elements: A Guideline

Section titled “Attributes vs. Child Elements: A Guideline”

A common question is when to store information as an attribute versus a child element. There’s no single strict rule, but here are some widely accepted guidelines:

Consider these two ways to represent the same information:

Using an attribute for gender:

<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

Using an element for gender:

<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

When to prefer attributes:

  • Metadata: Information about the element’s content, not the content itself (e.g., id, status, unit, language).
  • Simple, atomic values: Data that won’t contain substructure or multiple values.
  • Identifiers: Unique IDs used for referencing the element (standard practice uses xml:id).
  • Configuration flags: Simple true/false or enumerated values controlling behavior.

When to prefer child elements:

  • Core Content: The primary data the element represents.
  • Structured Data: Information that has its own internal structure (e.g., an address with street, city, zip).
  • Multiple Values: If an item can occur more than once (e.g., multiple phone numbers).
  • Readability/Maintainability: If putting too much into attributes makes the start tag long and hard to read.
  • Potential for Future Expansion: Elements are generally easier to expand with more sub-elements later.

Option 1: Date as an attribute (Simple, but less structured)

<note date="2024-07-01">
...
</note>

Option 2: Date as a simple element (Clearer separation)

<note>
<date>2024-07-01</date>
...
</note>

Option 3: Date as a structured element (Most flexible, easiest to parse specific parts)

<note>
<date>
<year>2024</year>
<month>07</month>
<day>01</day>
</date>
...
</note>

The best choice often depends on the specific context and how the data will be used.

A primary use case for attributes is storing metadata, such as unique identifiers. The standard attribute for this is xml:id.

<?xml version="1.0" encoding="UTF-8"?>
<messages xmlns:xml="http://www.w3.org/XML/1998/namespace">
<note xml:id="msg501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note xml:id="msg502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
</messages>

Here, xml:id provides a unique identifier for each note, which is data about the note, not part of the message content itself. This is an appropriate use of an attribute.

In summary, use attributes judiciously for metadata and simple configuration, and prefer elements for core content and structured data.