Latest web development tutorials

XML attribute

XML elements can have attributes similar to HTML.

Attribute (Attribute) provides additional information about the element.


XML attribute

In HTML, attributes provide additional information about the element:

<img src="computer.gif">
<a href="demo.html">

Properties are not usually provide information about the data part. In the following example, the file type has nothing to do with the data, but need to handle this element of software is very important:

<file type="gif">computer.gif</file>


XML attributes must be quoted

Attribute values ​​must be surrounded by quotation marks, although single or double quotes can be used. For example, a person's gender, person element can be written:

<person sex="female">

Or this may be:

<person sex='female'>

If the attribute 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 &quot;Shotgun&quot; Ziegler">


XML elements vs. attributes

Consider 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 an attribute. In the second instance, sex is an element. Both examples provide the same information.

No rule can tell us when to use attributes, and when to use elements. My experience is in HTML, the property is very convenient to use, but in XML, you should try to avoid the use of property. If the information feels like data, use elements of it.


My favorite way

The following three XML documents contain exactly the same information:

The first example uses the date properties:

<note date="10/01/2008">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The second example uses the date elements:

<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 third example uses the extended date element (which 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 attribute?

Some of the problems caused by the use of property:

  • Attributes can not contain multiple values ​​(elements can)
  • Attributes can not contain tree structures (elements can)
  • Attributes are not easily expandable (for future changes)

Property is difficult to read and maintain. Try to use elements to describe data. But only use the property to provide data independent information.

Do not do such a stupid thing (This is not the 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 metadata for the property

Sometimes ID references are assigned to the elements. The ID can be used to identify XML element index, the way it works in HTML id attribute is the same. This example demonstrated to 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 above is just an id attribute identifier is used to identify the different notes. It is not part of the notes data.

Here we tried to pass your philosophy is: metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.