Monday, July 4, 2011

XML ATTRIBUTES

XML Attributes

XML elements can have attributes, just like HTML.

Attributes provide additional information about an element.

In HTML, attributes provide additional information about elements:


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

PLEASE Remove Firstly All Asterics *


Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element:


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

PLEASE Remove Firstly All Asterics *



Quotes

You must place quotation marks around the attribute's value.


Wrong:


<*tutorials type=Web>
<*tutorial>
<*name>XML
<*/tutorial>
<*/tutorials>

Right:


<*tutorials type="Web">
<*tutorial>
<*name>XML
<*/tutorial>
<*/tutorials>

PLEASE Remove Firstly All Asterics *



Shorthand Is Prohibited

Attributes must contain a value. Some HTML coders like to use shorthand, where if you provide the attribute name without a value, it will equal true. This is not allowed in XML.


Wrong:


<*tutorials published>
<*tutorial>
<*name>XML
<*/tutorial>
<*/tutorials>

Right:


<*tutorials published="true">
<*tutorial>
<*name>XML
<*/tutorial>
<*/tutorials>

PLEASE Remove Firstly All Asterics *



Avoid XML Attributes?

Some of the problems with using attributes are:

* attributes cannot contain multiple values (elements can)
* attributes cannot contain tree structures (elements can)
* attributes are not easily expandable (for future changes)

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.

Don't end up like this:


<*note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!"*>

PLEASE Remove Firstly All Asterics *




XML Attributes for Metadata

Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the id attribute in HTML. This example demonstrates this:


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

PLEASE Remove Firstly All Asterics *


The id attributes above are for identifying the different notes. It is not a part of the note itself.

What I'm trying to say here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.

THANK YOU