XML Attributes
XML elements have attributes, similar to HTML.
Attributes provide additional information about elements.
XML Attributes
In HTML, attributes provide additional information about elements:
<a href="demo.html">
Attributes usually provide information that is not part of the data. In the example below, the file type is not related to the data, but it is very important to the software that needs to handle this element:
XML Attributes Must Be Quoted
Attribute values must be surrounded by quotes, though both single and double quotes can be used. For example, for a person's gender, the person element can be written like this:
Or like this:
If the attribute value itself contains double quotes, you can use single quotes, as in this example:
Or you can use character entities:
XML Elements vs. Attributes
Look at these examples:
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
In the first example, sex is an attribute. In the second example, sex is an element. Both examples provide the same information.
There are no rules telling us when to use attributes and when to use elements. My experience is that attributes are convenient in HTML, but in XML you should try to avoid using attributes. If information feels like data, then use elements.
My Favorite Way
The following three XML documents contain exactly the same information:
The first example uses the date attribute:
<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:
<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 an extended date element (this is my favorite):
<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>
Avoid XML Attributes?
Some problems caused by using attributes:
- Attributes cannot contain multiple values (elements can)
- Attributes cannot contain tree structures (elements can)
- Attributes are not easy to extend (for future changes)
Attributes are difficult to read and maintain. Please try to use elements to describe data. Only use attributes to provide information that is not related to data.
Do not do such a silly thing (this is not the way XML should be used):
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
XML Attributes for Metadata
Sometimes ID references are assigned to elements. These ID indexes can be used to identify XML elements, and they work in the same way as the id attribute in HTML. This example demonstrates this:
<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 strongly convey here is: metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.
Other extensions