XML Elements vs. Attributes

For the attributes and the child elements in XML, there are no rules about when to use which one.

Use of Elements vs Attributes:

We can store the data either in child elements or in attributes.

Example 1:

<employee gender="male">
  <firstname>Tom</firstname>
  <lastname>Gates</lastname>
</employee>

Example 2:

<employee>
  <gender>male</gender>
  <firstname>Tom</firstname>
  <lastname>Gates</lastname>
</employee>

Explanation:

In the above examples, gender is an attribute in the first one and a child element in the second one, however providing the same information.

Recommended Way:

We can use either of the attributes or child elements. The attributes can be handy in HTML. In XML, however, we should try to avoid them, and if the information feels like data, we can use the child elements. What we recommend is that the data itself should be stored as elements, and only the metadata, i.e., the data about data should be stored as attributes.

Example 1:

<note date="1/8/2020">
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>

Example 2:

<note>
  <date>1/8/2020</date>
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>

Example 3: RECOMMENDED WAY:

<note>
  <date>
    <day>1</day>
    <month>8</month>
    <year>2020</year>
  </date>
 <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>

Explanation:

In the above examples, all three XML documents contain the same information. However, a date attribute is used in the first example, a date element is used in the second example and an expanded date element is used in the third example.

Why to avoid using attributes?

Some of the limitations of attributes are:

  • They cannot contain multiple values, but the child elements can.
  • They are not easily expandable for future changes.
  • They cannot describe structures, but the child elements can.
  • They are more difficult to manipulate by program code than the child elements.
  • Their values are not easy to test against a DTD.

Using attributes as containers for data results in a document that is difficult to read and maintain. It is recommended to use the child elements to describe data, and the attributes to provide information that is not relevant to the data.

Incorrect Way:

XML should not be used like this:

<note day="1" month="8" year="2020"
to="Sapna" from="Tom" heading="Message"
body="Meeting on Monday at 11 AM.">
</note>

Exception:

The ID references can be assigned to the elements, and can then be used to access the XML elements in much the same way as the NAME or ID attributes in HTML.

Example:

<messages>
<note id="id1">
  <to>Sapna</to>
  <from>Tom</from>
  <heading>Message</heading>
  <body>Meeting on Monday at 11 AM.</body>
</note>
 
<note id="id2">
  <to>Tom</to>
  <from>Sapna</from>
  <heading>Re: Message</heading>
  <body>Thanks for the reminder!</body>
</note>
</messages>

Explanation:

In the above example, ID is used as a counter, or a unique identifier, to identify the different notes in the XML file. Here, the ID is not a part of the note data.

Please Share