May 27, 2021 XML
XML elements have properties, similar to HTML.
Attribute provides additional information about the element.
In HTML, properties provide additional information about elements:
<img src="computer.gif">
<a href="demo.html">
Properties typically provide information that is not part of the data. In the following example, the file type is data-independent, but important for software that needs to work with this element:
<file type="gif">computer.gif</file>
Property values must be surrounded by quotation marks, but both single and double quotes are available. For example, a person's gender, the person element can be written like this:
<person sex="female">
Or you can:
<person sex='female'>
If the property value itself contains double quotes, you can use single quotes, like this example:
<gangster name='George "Shotgun" Ziegler'>
Or you can use character entities:
Take a 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 instance, sex is a property. I n 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. M y experience is that in HTML, properties are convenient to use, but in XML, you should try to avoid using properties. If the information feels like data, use elements.
The following three XML documents contain exactly the same information:
The date property was used in the first instance:
<note date="10/01/2008">
<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 instance:
<note>
<date>10/01/2008</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 the third instance (this is my favorite):
<note>
<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>
Some of the problems caused by using properties:
Properties are difficult to read and maintain. T ry to use elements to describe the data. Property is used only to provide data-independent information.
Don't do such stupid things (this is not how XML should be used):
<note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
Sometimes an ID reference is assigned to an element. T hese ID indexes can be used to identify XML elements in the same way that the id property in HTML works. This example shows us this situation:
<messages>
<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 id property above is simply an identifier that identifies different notes. It is not part of the note data.
The idea that we are trying to convey to you here is that metadata (data about the data) should be stored as attributes and the data itself should be stored as elements.
Here are the rules to follow for summary XML properties: