XML Attributes

XML elements can have attributes in the start tag, just like HTML. Attributes are used to provide additional information about elements.

XML Attributes

XML Attributes

     

XML elements can have attributes in the start tag, just like HTML. Attributes are used to provide additional information about elements. Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:

<file type="gif">roseindia.gif</file>

Use the quote styles: "red" or 'red'

Attribute values must always be enclosed in quotes. Use either single or double quotes  eg..

<color="red">

or like this:

<color='red'>

Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example: 

<name='Rose "India" Net'>

Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example: 

<name="Rose 'India' Net">

Use of Elements vs. Attributes

If you start using attributes as containers for XML data, you might end up with the documents that are both difficult to maintain and  manipulate. So the user should use elements to describe the data. Use attributes only to provide data that is not relevant to the reader. Only metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.

This is not the way to use attributes eg..

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail To="Rohan" From="Amit" Subject="Surprise...."
<Body>Be ready for a cruise...i will catch u  tonight</Body>
</E-mail>

Try to avoid using attributes in few of the situations.

Lot of  problems occur with using attributes values. They are not easily expandable and cannot contain multiple values .They are not easy to test against a Document Type Definition and are also unable to describe their structure. Becomes more  irritating ,because of its difficultly to get manipulated by program code.

Here is an example, demonstrating how elements can be used instead of attributes. The following three XML documents contain exactly the same information. A date attribute is used in the first, a date element is used in the second, and an expanded date element is used in the third:

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail date="15/05/07">
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u  tonight</Body>
</E-mail>

First  xml document contains date as a attribute which can not  be further extended. But date used a element in second document makes it more flexible.

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail >
<date="15/05/07">
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u tonight</Body>
</E-mail>

Second xml document can be further extended as.. 

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail >
<date> 
  <day>12</day> 
  <month>11</month> 
  <year>99</year> 
</date>

<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u tonight</Body>
</E-mail>