<img src="computer.gif"><a href="demo.html">
Attributes often provide information that is not part of the data. In the following example, the file type has nothing to do with the data, but is important to the software that needs to handle this element:
<file type="gif">computer.gif</file>
Attribute values must be surrounded by quotes, but both single and double quotes can be used. For example, a person's gender, the person element can be written like this:
<person sex="female">
Or this also works:
<person sex='female'>
If the attribute value itself contains double quotes, you can use single quotes, like in this example:
<gangster name='George "Shotgun" Ziegler'>
Or you can use character entities:
<person sex="female"><firstname>Anna</firstname><lastname>Smith</lastname></person>
<person><sex>female</sex><firstname>Anna</firstname><lastname>Smith</lastname></person>
In the first instance, sex is a property. In the second instance, sex is an element. Both instances provide the same information.
There are no rules that tell us when to use attributes and when to use elements. My experience is that in HTML, attributes are very convenient to use, but in XML, you should try to avoid using attributes. If information feels a lot like data, use elements.
The following three XML documents contain exactly the same information:
The date attribute is used in the first example:
<note date="10/01/2008"><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body ></note>
The second example uses the date element:
<note><date>10/01/2008</date><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend! </body></note>
The third example uses the extended date element (this is my favorite):
<note><date><day>10</day><month>01</month><year>2008</year></date><to>Tove</to><from>Jani</from> <heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
Some problems caused by using attributes:
Attributes cannot contain multiple values (elements can)
Attributes cannot contain tree structures (elements can)
Properties are not easily extendable (for future changes)
Properties are difficult to read and maintain. Please try to use elements to describe data. Instead, just use attributes to provide data-independent information.
Don't do something stupid like this (this is not the way XML should be used):
<note day="10" month="01" year="2008"to="Tove" from="Jani" heading="Reminder"body="Don't forget me this weekend!"></note>
Sometimes ID references are assigned to elements. These ID indexes can be used to identify XML elements in the same way as the id attribute in HTML. This example demonstrates this situation to us:
<messages><note id="501"><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body> </note><note id="502"><to>Jani</to><from>Tove</from><heading>Re: Reminder</heading><body>I will not</body></ note></messages>
The id attribute above is just an identifier used to identify different notes. It is not part of the note data.
The idea we're trying to convey to you here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.
Here are the rules that summary XML attributes need to follow:
Attribute names cannot appear once in the same start tag or empty element tag
A property must be declared using the document type definition (DTD) declared in the property sheet.
Property values cannot contain direct or indirect entity references to external entities
The replacement text of any entity called a direct or indirect attribute value cannot contain any less than sign