Skip to main content

XML Attributes

In XML, attributes are used to provide additional information, or metadata, about an element.

They are always placed inside an element's start-tag and are written as name="value" pairs.

Attributes are a fundamental part of XML syntax, allowing you to add properties to your elements without creating new child elements for them.

Basic Syntax of an Attribute​

An XML attribute is defined as a name-value pair, specified within the start-tag of an element.

<element-name attribute1="value1">
...content...
</element-name>

Example: category is an attribute of the <book> element, providing metadata about what type of book it is.

<book category="web">
<title>Learning XML</title>
</book>

An element can have multiple attributes, separated by spaces.

<book category="web">
<title lang="en" version="1">Learning XML</title>
</book>
note

"value" must be single ('value') or double ("value") quoted.

Single Quoted Attribute​

<person gender="female"/>
<book publisher="McGraw Hill" category="Computer"/>

Double Quoted Attribute​

<person gender='female'/>
<book publisher='McGraw Hill' category='Computer'/>

Attribute with both single and double quotes​

If the value of the attribute itself contains double quotes, you can use single quotes:

<character name="Martin 'Marty' Seamus McFly"/>

viceversa, if the value of the attribute contains single quotes, you can use double quotes:

<character name='Martin "Marty" Seamus McFly'/>

Otherwise, you can use the character entities for double quotes:

<character name="Martin &quot;Marty&quot; Seamus McFly"/>

Attributes vs. Elements: A Critical Design Choice​

One of the most common questions when designing an XML structure is whether to store a piece of data as an attribute or as a child element. Both of the following examples are syntactically valid and represent the same information:

As an Attribute:

<person gender="female"/>

As a Child Element:

<person>
<gender>female</gender>
</person>

There are no strict rules, but there are strong conventions and practical reasons to choose one over the other.

When to Use Attributes​

Attributes are best used for metadata, that is, data about the element's content, rather than the content itself. Good candidates for attributes are:

  • ID numbers (e.g., <product id="prod101">).
  • Language codes (e.g., <title lang="en">).
  • Simple, single values that describe the element (e.g., <book category="fiction">).

When to Use Child Elements​

Child elements are more flexible and should be preferred in most other cases. Use child elements when the data:

  • Is the content itself: The name "Alice Smith" is the core data, not metadata about a person.

    • Good: <person><name>Alice Smith</name></person>
    • Bad: <person name="Alice Smith"/>
  • Contains complex data or a tree structure: Attributes cannot contain other elements.

    • Good:
      <person>
      <address>
      <street>123 Main St</street>
      <city>Anytown</city>
      </address>
      </person>
    • Bad: <person address="123 Main St, Anytown"/>
  • May contain multiple values: An attribute can only have one value.

    • Good:
      <person>
      <phone>555-1234</phone>
      <phone>555-5678</phone>
      </person>
    • Bad: <person phone="555-1234, 555-5678"/>
  • Needs to be easily expandable: It's easier to add a new child element (e.g., <middle_name>) than to restructure an attribute.

Prefer XML Elements to XML Attributes?​

A good guideline to follow is: Data that is part of the information should be an element. Data about the information should be an attribute.

Notice that:

  • attributes can't contain multiple values (while elements can).
  • attributes can't contain tree structures (while elements can).
  • attributes aren't easily expandable for future changes.
  • attributes are more difficult to be manipulated by program code.
  • attribute values are not easy to test against a DTD (which is used to define the legal elements of an XML document).

Element Attribute Rules​

XML Attributes must follow these rules:

  • An attribute name must not appear more than once in the same start-tag or empty-element tag.
  • An attribute must be declared in the Document Type Definition (DTD) using an Attribute-List Declaration.
  • Attribute values must not contain direct or indirect entity references to external entities.
  • The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a less than sign (<)

Conclusion​

XML attributes are a vital tool for adding descriptive metadata to your elements.

  • They are always written as name="value" pairs inside the start-tag.
  • Their values must always be quoted, and their names are case-sensitive.
  • As a general rule, use attributes for metadata and elements for the core data content.

By using attributes and elements appropriately, you can create XML documents that are not only well-formed but also well-designed, readable, and easy to maintain.