XML Attributes
XML Attributes
Attribute values must always be quoted. Either single or double quotes can be used.
For a person's gender, the <person> element can be written like this:
<person gender="female">
or like this:
<person gender='female'>
If the attribute value itself contains double quotes you can use single quotes, like in this example:
<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
In the first example, gender is an attribute. In the last example, gender is an element. Both examples
provide the same information.
There are no rules about when to use attributes or when to use elements in XML.
ADVERTISEMENT
My Favorite Way
The following three XML documents contain exactly the same information:
<note date="2008-01-10">
<to>Tove</to>
<from>Jani</from>
</note>
<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
</note>
<note>
<date>
<year>2008</year>
<month>01</month>
<day>10</day>
</date>
<to>Tove</to>
<from>Jani</from>
</note>
Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements
in much the same way as the id attribute 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 attributes above are for identifying the different notes. It is not a part of the note itself.
What I'm trying to say here is that metadata (data about data) should be stored as attributes, and the
data itself should be stored as elements.