XML element vs. Property


In XML, there are no rules specifying when to use attributes and when to use child elements.


Using Elements vs. Attributes

Data can be stored in child elements or in attributes.

Let's 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 latter example, "sex" is a child element. However, both provide the same information.

There are no special rules about when to use attributes and when to use child elements. In my experience, attributes are used more in HTML, but in XML, using child elements feels more like data information.


The way I like it

I like to store data in child elements.

The following three XML documents contain exactly the same information:

This example uses a "date" attribute:

<note date="12/11/2002">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

This example uses a "date" element:

<note>
  <date>12/11/2002</date>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

This example uses an extended "date" element: (this is my favorite way):

<note>
  <date>
    <day>12</day>
    <month>11</month>
    <year>2002</year>
  </date>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>


Avoid using attributes?

Should you avoid using attributes?

Some attributes have the following problems:

  • Attributes cannot contain multiple values (child elements can)
  • Attributes are not easily expandable (for future changes in requirements)
  • Attributes cannot describe structure (child elements can)
  • Attributes are more difficult to manipulate in program code
  • Attribute values are not easy to test against a DTD

If you use attributes as data containers, the resulting XML document will be difficult to read and maintain. Try to useelementto describe data. We only recommend using attributes when the data provided is irrelevant information.

Don't end up like this (this is not how XML should be used):

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


An exception to the attribute rule

Rules always have exceptions.

There is one exception to my rules about attributes.

Sometimes I specify an ID applied to an element. These IDs can be used to access XML elements in many of the same situations as NAME or ID attributes in HTML. The following example illustrates this approach:

<messages>
<note id="p501">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

<note id="p502">
  <to>Jani</to>
  <from>Tove</from>
  <heading>Re: Reminder</heading>
  <body>I will not!</body>
</note>
</messages>

In the XML file in the example above, the ID is just a counter, or a unique identifier, used to identify different notes rather than being part of the data.

What I want to say here is that metadata (data about data) should be stored as attributes, while the data itself should be stored as elements.

other extensions