The final leg in the journey in Understanding the Basic Rules of XML is attributes and special characters. An attribute in the language of XML works similar to attributes in HTML. An attribute provides additional information about an element. Like all things XML, there are rules to writing attributes. If you missed out on the first three articles in this series, check out the XML tutorials for declarations, root elements, child elements and data strings here: The Declaration and Root Element, Adding Child Elements and Adding Data Strings and Comments.
XML Rule #5 - Attribute Values Must be Contained in Quotes
The concept of attributes works the same in XML as it does in HTML. Attributes present in name/value pairings, let's look at an attribute for the message document.
<?xml version= "1.0">
<message>
<Dick date= "01/01/2010"> - element and attribute
Hi there Dick, this is the data I am sending you.
</Dick>
<Jane date= "01/02/2010">
Hi Jane, I am sending you different data.
</Jane>
</message>
The attribute added is 'date.' The value of the attribute is '01/01/2010.' Attributes are a consistent theme in any programming language. It is a way of applying a label to information about the element. In this situation, the date attribute explains the day this message post.
XML Rule #6 - Do Not Use Special Characters in Element Names
Some characters have special meaning to XML. You have seen a number of them already. You would not use '‹' in an element name (‹‹message›) because '‹' is the way XML identifies an element. When creating names for attributes or elements, stay away from these characters:
< - less than
> - greater than
& - ampersand
' - apostrophe
" - quotation mark
If you use one of these special characters in a name, the browser will not understand the script.
That wraps up some basic information you need to know to produce a viable XML page. This is certainly not all there is to know. The ease of writing in XML is one advantage to its use as a programming tool, but simplicity does not mean fixed or inflexible. Future articles will discuss more about the specifics of this format in detail. So stay tuned and learn about the role of XML in website design.
