Why to Avoid
XML Attributes?
Some of the problems with using
attributes are:
- attributes cannot contain multiple values (elements can)
- attributes cannot contain tree structures (elements can)
- attributes are not easily expandable (for future changes)
- Attributes are difficult to read and maintain.
- Use elements for data.
- Use attributes for information that is not relevant to the data.
Don't end up like this:
<note day="10" month="01" year="2008"
to="Tom" from="Jerry" heading="Reminder"
body="Don't forget Birthday Party this weekend!">
</note>
to="Tom" from="Jerry" heading="Reminder"
body="Don't forget Birthday Party this weekend!">
</note>
XML
Attributes for Metadata
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="201">
<to>Tom</to>
<from>Jerry</from>
<heading>Reminder</heading>
<body>Don't forget Birthday Party this weekend!</body>
</note>
<note id="202">
<to>Jerry</to>
<from>Tom</from>
<heading>Re: Reminder</heading>
<body>I will not be available to make it to Birthday Party</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.