Latest web development tutorials

XML elements and attributes comparison

In XML, and there are no provisions when to use attributes, and when to use child elements.


Use elements vs. attributes

Data can be stored in child elements or attributes.

Let's look at these examples:

<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

In the first example, "sex" is an attribute. In one example, back in, "sex" is a sub-element. But both provide the same information.

No special provisions for when to use attributes, and when to use child elements. My experience is that the use of multi-attribute in HTML heavy, but in XML, the use of sub-elements, it will feel more like data.


I like the way

I like to store data in child elements

The following three XML documents contain exactly the same information:

This example uses a "date" attribute:

<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

This example uses a "date" element:

<note>
<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 extension "date" element: (This is my favorite way):

<note>
<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?

You should avoid using attributes?

Some attribute has the following problems:

  • Attributes can not contain multiple values ​​(child elements can)
  • Attributes are not easily expandable (for the future needs change)
  • Attributes can not describe structures (child elements can)
  • Attributes are more difficult to manipulate the program code
  • Attribute values ​​are not easy to test for DTD

If you use the property as a data container, the resulting XML document will be difficult to read and maintain. Try to useelements to describe data.to describe data. Data is only providing irrelevant information when we recommend the use of property.

Do not end like this (which is not XML should be used):

<note day="12" month="11" year="2002"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>


Exception to a rule of property

There is always another rule

Rules on property I have an exception.

Sometimes I use elements specified ID. These applications can be used as ID NAME attribute or ID to access XML elements in HTML in the same situation a lot. The following example illustrates this approach:

<messages>
<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>

XML documents in the above examples, the ID of only one counter, or a unique identifier to identify the different notes, rather than as part of the data.

I want to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.