Attributes are used to provide additional information
about elements.
From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.
In HTML (and in XML) attributes provide additional information about
elements:
<img src="computer.gif"> <a href="demo.asp"> |
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">computer.gif</file> |
<person sex="female"> |
or like this:
<person sex='female'> |
Double quotes are the most common, but sometimes (if the attribute value
itself contains quotes) it is necessary to use single quotes, like in this
example:
<gangster name='George "Shotgun" Ziegler'> |
Take a look at these examples:
<person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person> |
<person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname> </person> |
In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.
There are no rules about when to use attributes, and when to use child
elements. My experience is that attributes are handy in HTML, but in XML
you should try to avoid them. Use child elements if the information feels
like data.
The following three XML documents contain exactly the same information:
A date attribute is used in the first example:
<note date="12/11/99"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
A date element is used in the second example:
<note> <date>12/11/99</date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
An expanded date element is used in the third: (THIS IS MY FAVORITE):
<note> <date> <day>12</day> <month>11</month> <year>99</year> </date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
Here are some of the problems using attributes:
Don't end up like this ( if you think this looks like XML, you have
not understood the point):
<note day="12" month="11" year="99" to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!"> </note> |
My rule about attributes has one exception:
Sometimes I assign ID references to elements. These ID references can
be used to access XML elements in much the same way as the NAME or ID attributes
in HTML. This example demonstrates this:
<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 in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.