XML Attributes

XML elements can include attributes in the start tag, similar to HTML.

Attributes (Attribute) provide additional (additional) information about elements.

XML Attributes Must Be Quoted

Attribute values must be enclosed in quotes, and both single and double quotes can be used.

For example, to represent a person's gender, the <person> tag can be written as:

<person gender="female">

Or it can also be like this:

<person gender='female'>

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

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

Or you can use character entities:

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

XML Elements and Attributes

Please see these two examples:

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

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

There are no rules in XML that tell us when to use attributes and when to use child elements. My experience is that attributes are very convenient to use in HTML, but in XML, you should try to avoid using attributes. If the information seems like data, then use child elements.

My favorite way

The following three XML documents contain completely the same information:

In the first example, the date was used as an attribute:

<note date="2008-01-10">
  <to>George</to>
  <from>John</from>
</note>

In the second example, the <date> element was used:

<note>
  <date>2008-01-10</date>
  <to>George</to>
  <from>John</from>
</note>

In the third example, an extended date element was used (which is my favorite):

<note>
  <date>
    <year>2008</year>
    <month>01</month>
    <day>10</day>
  </date>
  <to>George</to>
  <from>John</from>
</note>

Avoid using XML attributes?

Some considerations when using attributes are:

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

Try to use elements to describe data. And just use attributes to provide information unrelated to data.

Don't do such silly things (this is not the way XML should be used):

<note day="10" month="01" year="2008"
to="George" from="John" heading="Reminder"
body="Don't forget the meeting!"
</note>

XML attributes for metadata

Sometimes, IDs are assigned to elements for reference. These ID indices can be used to identify XML elements, working in the same way as the ID attribute in HTML. This example demonstrates this situation:

<messages>
  <note id="501">
    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
  </note>
  <note id="502">
    <to>John</to>
    <from>George</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
  </note> 
</messages>

The above ID is just an identifier used to identify different tags. It is not part of the tag data.

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