XML attributes
XML elements have attributes, similar to HTML.
Attributes provide additional information about elements.
XML Attributes
In HTML, attributes provide additional information about an element:
<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:
XML attributes must be quoted
Attribute values must be surrounded by quotes, but both single and double quotes can be used. For example, for a person's gender, the person element can be written like this:
Or this can also be written:
If the attribute value itself contains double quotes, you can use single quotes, like this example:
Or you can use character entities:
##XML Elements vs. AttributesSee these examples:
<lastname>Smith</lastname>
</person>
##<person>
<lastname>Smith</lastname>
</person>
In the first instance, sex is an attribute. 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 convenient to use, but in XML, you should try to avoid using attributes. If information feels a lot like data, use elements.
My favorite way
The following three XML documents contain exactly the same information:
The date attribute is used in the first example:
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The date element is used in the second example:
<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 mine 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 a tree structure (elements can)
- Attributes are not easily extensible (for future changes)
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 in the same way as the id attribute in HTML. This example demonstrates this to us:
<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 above id attribute is just an identifier, used to identify different notes. It is not part of the note data.
The idea we are 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.