![]() |
|
[XML Attributes][Well Formed XML][DTD/CDATA] XML Attributes XML elements can have attributes. From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element. In HTML (and in XML) attributes provide additional information about elements:
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 important to the software that wants to manipulate the element:
Data can be stored in elements or in attributes. Take a look at these examples:
In the first example sex is an attribute. In the last, sex is an element. Both examples provides the same information. There are no rules about when to use attributes, and when to use elements. My experience is however; that attributes are handy in HTML, but in XML you should try to avoid them. Use elements if the information feels like data. My Favorite Way I like to store data in elements. The following three XML documents contain exactly the same information: A date attribute is used in the first example:
An expanded date element is used in the third: (THIS IS MY FAVORITE):
Avoid using attributes? Should you avoid using attributes? Here are some of the problems using attributes:
If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data Don't end up like this ( if you think this looks like XML, you have not
understood the point):
Rules always have exceptions. My rule about attributes has one too: Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data. |
|
![]() |