The XML property


May 27, 2021 18:00 XML


Table of contents


The XML property

This section explains the properties of XML, which are contained in an XML element, and an XML element can have multiple properties.
This chapter describes XML properties. T he property is part of the XML element. A n element can have multiple unique properties. Properties provide more information about XML elements XML properties are always a name value pair

XML elements have properties, similar to HTML.

Attribute provides additional information about the element.


The XML property

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>


XML properties must be quoted

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:

<gangster name="George "Shotgun" Ziegler">


XML elements vs. properties

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.


My favorite way

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>


Avoid XML properties?

Some of the problems caused by using properties:

  • The property cannot contain more than one value (elements can)
  • Properties cannot contain tree structures (elements can)
  • Properties are not easy to extend (for future changes)

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>


XML properties for metadata

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:

  • Property names cannot appear once in the same starting or empty element labels
  • A property must use a document type definition (DTD) declaration declared by a property sheet
  • Property values cannot contain direct or indirect entity references to external entities
  • The replacement text of any entity is called a direct or indirect property value that cannot contain any less than the sign