Xml
You can store data in a child element or in an attribute. Take a look at the following 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, the gender sex is an attribute. In the second, sex is a child element. Two examples provide the same information. There are no special rules about when to use attributes and when to use child elements. My experience is that it's convenient to use attributes in HTML, but try to avoid using attributes in XML. If information is like data, use child elements.
The way I like it
I like to store data in child elements. The following 3 XML documents contain exactly the same information:
The first example uses a Date property:
< note date= "12/11/99" >
< to>tove</to>
< from>jani</from>
< heading>reminder< 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< Body>don ' t forget me this weekend!</body>
</note>
An extended date element was used in the third one (this is my favorite method):
< note>
< date>
< day>12</day>
< month>11</month>
< year>99</year>
</date>
< to>tove</to>
< from>jani</from>
< heading>reminder< Body>don ' t forget me this weekend!</body>
</note>
Do you want to avoid using attributes?
Should you avoid using attributes? Here are some of the problems with using attributes:
Attribute cannot contain multiple values (while child elements can)
Attributes are not easily expanded (for future modifications)
property cannot describe structure (while child elements can)
Property is more difficult to manipulate by program code
Attribute values are not easy to test for DTDs
If you use attributes as a container for data, the end result is that documents will be difficult to read and maintain. You should try to use elements to describe the data. Attributes are used only when data-independent information is provided.
Don't end this way (if you think this is XML, then you don't really understand the point):
< note day= "month=" year= "99"
To= "Tove" from= "Jani" heading= "Reminder"
body= "Don ' t forget me this weekend!" >
</note>