4. XML Attributes

What you will learn

  • What an attribute is and where it goes
  • The rules for attribute values
  • When to use an attribute and when to use an element
  • Useful special attributes

What is an attribute?

An attribute gives extra information about an element. It sits inside the start tag as a name="value" pair, just like in HTML.

XML code

<person id="101" status="active">
  <name>Sam</name>
</person>

What it means

A person whose id is 101 and whose status is active, with a child element name containing Sam.

The rules

  • The value must be in quotes, either double or single.
  • An element cannot have the same attribute twice.
  • Attribute names follow the same naming rules as elements, and are case sensitive.
  • Attributes have no fixed order.
  • Every attribute needs a value. There is no bare checked as in HTML.
  • A value cannot contain < or &. Escape them. To put a quote inside, use the other kind of quote, or &quot; and &apos;.

XML code

<note title='He said "hello"' mood="it's fine">Text</note>

Two ways to store the same data

XML code: using attributes

<book id="b1" language="en">
  <title>Learning XML</title>
</book>

XML code: using elements

<book>
  <id>b1</id>
  <language>en</language>
  <title>Learning XML</title>
</book>

Both are valid and carry the same facts. Which is better?

Attributes or elements?

Attributes are less flexible because… Elements are better when…
They can hold only one value, not a list The data may have several values
They cannot contain child structure The data may grow more detailed later
They are harder to read and change in documents The data is part of the actual content
They cannot be repeated The information appears more than once

A widely used rule of thumb:

  • Use elements for the data itself (a title, a price, a name).
  • Use attributes for metadata: facts about the data, such as an ID, a unit, a type, or a language.

XML code: a good mix

<product id="p-205">
  <name>Notebook</name>
  <price currency="USD">4.50</price>
</product>

The ID and the currency describe the product and the price. The name and amount are the data. This is a matter of style, not law, so follow any convention your project or standard uses.

Empty elements with attributes

When all the information is metadata, an empty element with attributes is neat.

XML code

<images>
  <image file="logo.png" width="120" height="60"/>
  <image file="banner.jpg" width="800" height="200"/>
</images>

Useful special attributes

Attribute Meaning
xml:lang The language of the content, such as en or ur
xml:space A hint about keeping whitespace
xmlns Declares a namespace (a later lesson)
id A unique name for an element. The rule that it must be unique is enforced when you use a schema or DTD

XML code

<greeting xml:lang="en">Hello</greeting>
<greeting xml:lang="fr">Bonjour</greeting>

Names that begin with xml are reserved, which is why you should not invent your own attributes that start that way.

A complete example

XML code: library.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book id="b1" available="true">
    <title xml:lang="en">Learning XML</title>
    <author>Sam Rivera</author>
    <price currency="USD">12.00</price>
  </book>
  <book id="b2" available="false">
    <title xml:lang="en">Learning PHP</title>
    <author>Ali Khan</author>
    <price currency="USD">15.50</price>
  </book>
</library>

The structure

library
 ├── book (id=b1, available=true)
 │    ├── title (xml:lang=en) = Learning XML
 │    ├── author = Sam Rivera
 │    └── price (currency=USD) = 12.00
 └── book (id=b2, available=false)
      ├── title (xml:lang=en) = Learning PHP
      ├── author = Ali Khan
      └── price (currency=USD) = 15.50

Note that available="true" holds the text “true”, not a real yes-or-no value. A schema (a later lesson) can say that it must be a boolean.

Common mistakes

  • Leaving out the quotes around a value.
  • Repeating an attribute on one element.
  • Writing HTML-style attributes with no value.
  • Using attributes for data that needs several values or its own structure.
  • Putting a raw & or < inside an attribute value.
  • Making up attribute names that begin with xml.

Exercise

Write an XML file for a movie. The root is movie, with an id attribute. Inside, put a title, and a length element that carries the number of minutes with a unit attribute set to minutes.

Show solution
<?xml version="1.0" encoding="UTF-8"?>
<movie id="m-77">
  <title>Night Train</title>
  <length unit="minutes">112</length>
</movie>

The ID and the unit are metadata, so they are attributes. The title and the length are the data, so they are elements.

Knowledge check

1. In which part of an element do attributes go?

In the start tag.

2. Must attribute values be quoted?

Yes, always, with single or double quotes.

3. What is a good rule for choosing between attributes and elements?

Use elements for the data itself and attributes for metadata about it, such as IDs and units.

Summary

  • Attributes are quoted name="value" pairs in the start tag, and cannot repeat on one element.
  • Prefer elements for data and attributes for metadata.
  • xml:lang and xmlns are special reserved attributes.
Scroll to Top