YouTip LogoYouTip

Xml Attributes

XML Attributes | Rookie Tutorial


XML elements have attributes, similar to HTML.

Attributes provide additional information about elements.


XML Attributes

In HTML, attributes provide additional information about elements:

<img src="computer.gif">
<a href="demo.html">

Attributes usually provide information that is not part of the data. In the example below, the file type is not related to the data, but it is important for the software that needs to process this element:

<file type="gif">computer.gif</file>

XML Attributes Must Be Quoted

Attribute values must be enclosed in quotes, but either single or double quotes can be used. For example, for a person's gender, the person element can be written like this:

<person sex="female">

Or like this:

<person sex='female'>

If the attribute value itself contains double quotes, you can use single quotes, as in this example:

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

Or you can use character entities:

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

XML Elements vs. Attributes

Take a look at these examples:

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

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

In the first example, sex is an attribute. In the second example, sex is an element. Both examples provide the same information.

There are no rules about when to use attributes and when to use elements. In my experience, attributes are convenient in HTML, but in XML, you should try to avoid using attributes. If the information feels like data, then use elements.


My Favorite Way

The following three XML documents contain exactly the same information:

The first example uses the date attribute:

<note date="10/01/2008">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

The second example uses the date element:

<note>
  <date>10/01/2008</date>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

The third example uses an expanded date element (this is my favorite):

<note>
  <date>
    <day>10</day>
    <month>01</month>
    <year>2008</year>
  </date>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Avoid XML Attributes?

Some of the problems caused by using attributes:

  • Attributes cannot contain multiple values (elements can)
  • Attributes cannot contain tree structures (elements can)
  • Attributes are not easily expandable (for future changes)

Attributes are difficult to read and maintain. Please try to use elements to describe data. Only use attributes to provide information that is not relevant to the data.

Don't do something like this (this is not how XML should be used):

<note day="10" month="01" year="2008"
      to="Tove" from="Jani" heading="Reminder"
      body="Don't forget me this weekend!">
</note>

XML Attributes for Metadata

Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements, in the same way that the id attribute works in HTML. This example demonstrates this:

<messages>
  <note id="501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
  </note>
  <note id="502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
  </note>
</messages>

The id attribute above is just an identifier used to distinguish different notes. It is not part of the note data.

The idea we strongly advocate here is: metadata (data about data) should be stored as attributes, while the data itself should be stored as elements.

← Xml DtdXml Elements β†’