XML Attributes

Similar to HTML, XML elements can have attributes that are designed to contain data related to a specific element.

XML Attributes Must be Quoted:

Being always quoted, the attribute values can have either single or double-quotes.

For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<student gender="male">
</student>
<student gender="male"> </student>

or like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<student gender="male">
</student>
<student gender="male"> </student>

If the double quotes are already present in the attribute value itself then we can use single quotes.

For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<student name="Tom &quot;James&quot; Smith">
</student>
<student name="Tom &quot;James&quot; Smith"> </student>

or we can also use character entities to serve the same purpose:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<student name="Tom &quot;James&quot; Smith">
</student>
<student name="Tom &quot;James&quot; Smith"> </student>

XML Elements vs. Attributes:

Example 1:

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

  Tom
  Smith

Example 2:

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

  male
  Tom
  Smith

Explanation:

In the above examples, gender is an attribute in the first example and is an element in the second example. However, both have the same information. We can use either of the attributes or elements in XML.

The below three XML documents have the same information:

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note date="2020-06-04">
<to>Sapna</to>
<from>Tom</from>
</note>
<note date="2020-06-04"> <to>Sapna</to> <from>Tom</from> </note>

  Sapna
  Tom

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note>
<date>2020-06-04</date>
<to>Sapna</to>
<from>Tom</from>
</note>
<note> <date>2020-06-04</date> <to>Sapna</to> <from>Tom</from> </note>

  2020-06-04
  Sapna
  Tom

Explanation:

In the above example, we used a <date> element.

Example 3:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<note>
<date>
<year>2020</year>
<month>06</month>
<day>04</day>
</date>
<to>Sapna</to>
<from>Tom</from>
</note>
<note> <date> <year>2020</year> <month>06</month> <day>04</day> </date> <to>Sapna</to> <from>Tom</from> </note>

  
    2020
    06
    04
  
  Sapna
  Tom

Explanation:

In the above example, we used an expanded <date> element.

Things to consider while using XML attributes:

  • XML attributes cannot contain multiple values, but XML elements can.
  • XML attributes cannot contain tree structures, but XML elements can.
  • XML attributes are not easily expandable.

XML Attributes for Metadata:

The ID references assigned to the elements are used to identify XML elements. It is similar to the id attribute in HTML. For identifying the different notes, we use the id attributes, but they are not a part of the note itself. The metadata i.e., the data about data is to be stored as attributes. However, the data itself should be stored as elements.

Example:

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

  
    Sapna
    Tom
    Meeting
    Monday at 11 AM!
  
  
     Sapna
    Tom
    Re: Meeting
    Thanks for the reminder!