XML Attributes

In XML document, attributes make element more strong and meaningful. Element contain multiple attributes with unique name. It has name and value pair to define element in more broad way. Basically, it contain more details for elements or we can say it define more property for element. Attributes values must be in double quote or in single quote.

Similar like HTML tags has also attributes like element has.

Syntax for Attributes:

You can use following syntax to create attributes for element in XML document.

<ElementName Attributes1=”Value 1” Attributes2=”Value 2” Attributes3=”Value 3”>
.......Content part......
</ElementName >

ElementName:
Here, you can define meaningful name for your element.

Attributes:
Attributes1=”Value 1”, Attributes2=”Value 2” and Attributes3=”Value 3” are attributes or property for element. You can define name and value as well. Name must be unique label. It is necessary you can define value by using double quote (“ ”) or single quote (‘ ‘).

Let’s look into few more examples to understand attributes of element.

Example Valid or Not Valid
<product category=”Electronics” > </product> Valid
<product category1=”Electronics” category2=”Mobile” ></product> Valid
<product category1=”Electronics” category1=”Mobile” ></product> Not Valid
<student gender="Male"></student> Valid
<student gender='Male'></student> Valid with single quote as well
<student gender='Male”></student> Not Valid
<product categories=”Electronics,Mobile” ></product> Valid
<product categories=”Electronics” “Mobile” ></product> Not Valid

Attributes are normally used to specify more option for your element in XML document. When you don’t want to use too many element to define property or details for specified product, you can use attributes as better option.

Example 1:

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product color=”white” price=”$650.00” discount=”$20.00”>
<name>Sony Xperia R1 Dual</name>
</product>
</products>

Example 2:

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
</products>

In the above example1, you can see we have used color, price and discount as attribute for product element and defined suitable values for each of them.

In above both example give us same result so there is no any rules defined to use elements and attributes in XML document. You may use and define it as per your situation.

Lets understand it with one more example n different way.

Example 1:

<products>
<product categories=”Electronics”>
<name>Sony Xperia R1 Dual</name>
</product>
</products>

In above example, we have used categories as attribute for product element.

Example 2:

<products>
<product>
<name>Sony Xperia R1 Dual</name>
<categories>Electronics</categories>
</product>
</products>

In above example, we have used categories sub element for product element.

Example 3:

<products>
<product>
<name>Sony Xperia R1 Dual</name>
     <categories>
      <categories1>Electronics</categories1>
      <categories2>Mobiles</categories2>
     </categories>
</product>
</products>

In above example, we defined multiple categories for product by using <categories> element and <category1>, <category2> as sub elements.

Comparison between Elements and Attributes:

Elements Attributes
Element contain attributes Attribute doesn’t contain elements
Elements contain sub elements Attribute doesn’t contain sub attributes
Elements cane be expandable Attributes can be also expandable
You can create tree structure with elements You can create tree structure for attributes
Elements have name itself Attribute have name and value pair

Rules for Attributes:

You should follow below rules while using attributes in your XML document.

Attribute must be unique for single element.

It must have value pair.

Value of attributes must not contain any direct/ indirect entity references.

You must not use <category2> as value for attributes.