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:


or like this:


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

For example:


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


XML Elements vs. Attributes:

Example 1:


  Tom
  Smith

Example 2:


  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:


  Sapna
  Tom

Example 2:


  2020-06-04
  Sapna
  Tom

Explanation:

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

Example 3:


  
    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:


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

Please follow and like us:
Content Protection by DMCA.com