XML element and attribute comparison
XML Element vs Attribute
In XML, there are no regulations on when to use attributes, and when Use child elements.
Using elements vs attributes
Data can be stored in child elements or attributes.
Let’s take a 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 latter example, "sex" is a child element. But both provide the same information.
There are no special regulations on when to use attributes and when to use subelements. My experience is that in HTML, attributes are often used, but in XML, using sub-elements will feel more like data information.
The way I like it
I like to store data in child elements
The following three XML documents contain the exact same information :
The "date" attribute is used in this 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 this example:
<date>12/11/2002</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This example uses the extended "date" element: (This is my favorite way):
<date>
<day>12</day>
<month>11</month>
<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
##Avoid using attributes? Should you avoid using attributes?Some attributes have the following problems:
- Attributes cannot contain multiple values (child elements can)
- Attributes are not easy to expand (for future changes in requirements)
- Attributes cannot describe the structure (subelements can)
- Attributes are more difficult Manipulating program code
- Attribute values are not easy to test against DTD
elements to describe the data. to describe data. We recommend using attributes only when the data provided is irrelevant information.
Don't end up like this (this is not what XML should be used for):body="Don't forget me this weekend!">
</note>
Exception to an attribute rule
There is always another rule
I have an exception to the rule regarding attributes.
Sometimes the ID I specify is applied to the element. These ID applications can access XML elements as NAME or ID attributes in many of the same situations as in HTML. The following examples demonstrate this approach:
<note id="p501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note id="p502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>
</messages>
In the XML file of the above example, the ID is just a counter, or a unique identifier, to identify different notes, not as part of the data.
What I want to say here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.