Open In App

XML | Attributes

Prerequisite: XML | Basics
The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element.
Syntax: 
 

<element_name attribute1 attribute2 ... > Contents... </element_name>

In the above syntax element_name is the name of an element which can be any name. The attribute1, attribute2, … is XML attribute having unique attribute name. Then in the content section, any message can be written and at the end, the element name is ended.
Below some examples are given which illustrate the above syntax:
Example 1: 
 






<text category = "message">Hello Geeks</text>

In the above example, XML element is text, the category is the attribute name and message is the attribute value, Attribute name and its value always appear in pair. The attribute name is used without any quotation but attribute value is used in single ( ‘ ‘ ) or double quotation ( ” ” ).
Example 2: 
 




<text category = "message" purpose ="Greet">Hello Geeks</text>

In the above example, two attribute is used with different name. So, in a single element multiple attribute is used having unique attribute name. 
But if we use two distinct element then we can use the attribute having the same attribute name. This can be understood with the help of below example: 
Example 3: 
 






<text category = "message" >Hello Geeks.</text>
<text category = "message">How are you.</text>

Attribute Types: 
There are three types of attributes described below: 
 

Rules for creating an attribute: There are some rules that should be followed while creating an attribute: 
 

 


Article Tags :