Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML element and property comparison


May 28, 2021 DTD


Table of contents


XML elements vs. properties

XML elements: Refers to the part from the start label to the end label, the element can contain other elements, text, or a mixture of both, and the element can also have properties.

XML Properties: Provides additional information about elements.


In XML, there is no provision for when properties are used and when child elements are used.


Use the element vs. property

Data can be stored in child elements or properties.

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 a property. I n the latest example, "sex" is a child element. But both provide the same information.

There is no specific provision for when properties are used and when child elements are used. My experience is to use more attributes in HTML, but in XML, using child elements feels more like data information.


I like the way

I like to store data in child elements

The following three XML documents contain exactly the same information:

In this example, the "date" property is used:

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

In this example, the "date" element is used:

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

The extended "date" element is used in this example: (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 properties?

Should you avoid using properties?

Some properties have the following questions:

  • Properties cannot contain more than one value (child elements can)
  • Properties are not easy to extend (for future changes in demand)
  • Properties cannot describe structures (child elements can)
  • Properties make it more difficult to manipulate program code
  • Property values are not easy to test for DTD

If you use properties as a data container, the final XML documentation will be difficult to read and maintain. T ry using the element to describe the data. t o describe data. We recommend using properties only if the data provided is not relevant.

Don't end up like this (this is not what XML should use):

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

An exception to a property rule

There are always other rules

I have one exception to the rules about attributes.

Sometimes the ID I specify applies elements. T hese ID apps can access XML elements as NAME or ID properties in many of the same situations in HTML. The following example shows 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>

In the XML file of the above instance, 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.