Attributes in XML

What is the attributes in xml -: In the XML language attributes are the way to add additional data to the XML element. Attributes contain data in form of name and value pairs. Attributes are designed to contain data related to a specific element.

Attribute Types in XML-: There are three type in XML Attribute.

Attribute in xml

1-: String Type Attribute -: String Attribute takes any literal string. For example a value. CDATA is a String Type. CDATA is character data. It’s means any type string of non-markup characters is a legal part of the attribute.

2-: Tokenized Type Attribute -: This is a constrained type . The validity of constraints noted in the grammar are applied after the attribute value is normalized.

3-:Enumerated Type Attribute -: In this attribute has a predefined value in its declaration out of which it must assign one value.

Rules for writing the Attributes in xml -: Attribute are written in double quote. For example if we were writing fruits category then we write attribute in double quote. (“Mango”)” . Attribute are written as like the elements.There are some rules for writing the attributes.

1-: Attributes are name and value pairs. Only the attribute value must be in single or double quote not the name of attribute . For example

<fruit name="Mango">---</fruit>   // correct

<fruit "name=Mango">-- </fruit> // this is the incorrect

2.-:In XML element we can use more than one attributes. For understanding we take the example if we take fruit name mango and want to tell it is sweet then we can write in attribute as like

<fruit name="Mango Sweet">---</fruit>   // correct 

3. Attribute can not contain double value . For example

 <fruit name="Mango Sweet"  name="Mango sour">---</fruit>   // Incorrect  

XML Elements vs XML Attribute-: For understanding the xml attribute vs xml element we take a example .Here department is the Attribute

<education department="Books">

  <Name>C programming</Name>

  <Author> Netnic team </Author>


Here is the department is the elements

 <education >
  < department> Books </ department> 

  <Name>C programming</Name>

  <Author> Netnic team </Author>


Limitation of XML

In the both example the XML document contain the same data. However we should always prefer XML element over XML attribute. We learned that the same information can be contained in a XML document by using XML element in place of attribute. The reason we do this is because attributes have certain limitations in XML. These are the ..

  • Attributes cannot contain multiple values and the element have .
  • Using attributes we cannot achieve the XML tree structure that we can achieve using element and sub-elements.
  • attributes are not easily expandable and the elements are easily expandable.
  • Attributes values are difficult to test against a Document Type Definition.

Leave a Comment