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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<employee gender="male">
<firstname>Tom</firstname>
<lastname>Gates</lastname>
</employee>
<employee gender="male"> <firstname>Tom</firstname> <lastname>Gates</lastname> </employee>

  Tom
  Gates

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<employee>
<gender>male</gender>
<firstname>Tom</firstname>
<lastname>Gates</lastname>
</employee>
<employee> <gender>male</gender> <firstname>Tom</firstname> <lastname>Gates</lastname> </employee>

  male
  Tom
  Gates

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note date="1/8/2020">
<to>Sapna</to>
<from>Tom</from>
<heading>Message</heading>
Meeting on Monday at 11 AM.
</note>
<note date="1/8/2020"> <to>Sapna</to> <from>Tom</from> <heading>Message</heading> Meeting on Monday at 11 AM. </note>

  Sapna
  Tom
  Message
  Meeting on Monday at 11 AM.

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note>
<date>1/8/2020</date>
<to>Sapna</to>
<from>Tom</from>
<heading>Message</heading>
Meeting on Monday at 11 AM.
</note>
<note> <date>1/8/2020</date> <to>Sapna</to> <from>Tom</from> <heading>Message</heading> Meeting on Monday at 11 AM. </note>

  1/8/2020
  Sapna
  Tom
  Message
  Meeting on Monday at 11 AM.

Example 3: RECOMMENDED WAY:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note>
<date>
<day>1</day>
<month>8</month>
<year>2020</year>
</date>
<to>Sapna</to>
<from>Tom</from>
<heading>Message</heading>
Meeting on Monday at 11 AM.
</note>
<note> <date> <day>1</day> <month>8</month> <year>2020</year> </date> <to>Sapna</to> <from>Tom</from> <heading>Message</heading> Meeting on Monday at 11 AM. </note>

  
    1
    8
    2020
  
 Sapna
  Tom
  Message
  Meeting on Monday at 11 AM.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note day="1" month="8" year="2020" to="Sapna" from="Tom" heading="Message" body="Meeting on Monday at 11 AM.">
</note>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<messages>
<note id="id1">
<to>Sapna</to>
<from>Tom</from>
<heading>Message</heading>
Meeting on Monday at 11 AM.
</note>
<note id="id2">
<to>Tom</to>
<from>Sapna</from>
<heading>Re: Message</heading>
Thanks for the reminder!
</note>
</messages>
<messages> <note id="id1"> <to>Sapna</to> <from>Tom</from> <heading>Message</heading> Meeting on Monday at 11 AM. </note> <note id="id2"> <to>Tom</to> <from>Sapna</from> <heading>Re: Message</heading> Thanks for the reminder! </note> </messages>


  Sapna
  Tom
  Message
  Meeting on Monday at 11 AM.



  Tom
  Sapna
  Re: Message
  Thanks for the reminder!


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.