In the last four parts of this tutorial, I have shown you how to create a basic XML document and how it can be displayed in the browser. This section explains a few more XML techniques, and also provides a real-world usage of XML.

Attributes

Attributes are another way of storing data using XML. Up until now, we have just used very basic tags, surrounding information with tags which describe them. For example, this is the code we have been using so far:
<message>
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
<subject>Comments on XML</subject>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make much better use of the internet.
</body>
</message>

If you go back to thinking of XML as HTML, you will notice that this is made up completely of 'simple' tags. In HTML varient os tags are used which have attributes, for example to output text in the Arial font the following code would be used:
<font face="Arial">The text</font>

Similarly, in XML attributes can be used to store data. If I wanted, for example, to get rid of the subject tags in this example, I could use the following code:
<message subject="Comments on XML">
<header>
<from>webmaster@gowansnet.com</from>
<to>webmaster@xml.org</to>
</header>
<body>
I think that XML has great potential. It will work very well and will help many people to make much better use of the internet.
</body>
</message>

As you can see, I have used the attribute of the tag <message> to store the subject instead of it having its own tag.

This, although correct XML, would not really be a correct usage of the attributes of a tag. The attribute is used to give information about what is contained in the tag. Although it could be argued that it is telling you what the message is about, it would be more correct to provide this document in the original form, where there is a subject tag.

Although I have said that this would not really be a correct usage, you can use these fully interchangably, for example all the data for this e-mail message could have been stored as attributes of the message tag. To really benefit from XML, though, it is probably best to use attributes as little as possible, and to concentrate on structuring your documents correctly.

CDATA

One problem which becomes apparent when using XML is that the parser parses all data in an XML document. So in the following:
<body>Sales last year were less than sales this year</body>

Would be fully parsed by the parser, both the tags and text. This does not cause a problem, though. If this was written as:
<body>Sales last year < Sales this year</body>

This would cause a problem, because the XML parser would read this and think that the less than sign (<) in the text was the beginning of a new tag, so would cause an error. This can be overcome, though as, like HTML, XML has a variety of special codes for displaying these characters. There are 5 in XML:

Symbol

Code

<

&lt;

>

&gt;

="Arial, Helvetica, sans-serif">&

&amp;

'

&apos;

"

&quot;

By using these you can display the correct symbols, and the XML parser still works. So you could enter this text as:
<body>Sales last year &lt; Sales this year</body>

There are occasions, though, when you will have a lot of these special symbols in one section of your XML code, for example if you want to display programming code on your site. For this, the CDATA has been invented. This is like the HTML <xmp> tag, which causes the parser to ignore everything contained in it (so it misses all the special characters, but also will miss any tags contained in it). It is constructed as follows:
<![CDATA[
Text to be ignored
]]>

Real World Usage

After reading this whole tutorial, you may still be wondering what the point of XML is. It doesn't improve the look of your web page and the lack of browser support means that you can't use it as an alternative to a server-side database. There are uses which have been developed, though, although it will take a lot more development to make XML a mainstream language.

XMLNews is a system which allows news stories to be stored as XML. By using tags like <headline>, <byline>, <location> and <story> web pages and software systems can be developed which will take the XML data and will output it as a correctly formatted web page. In fact, the same story could be displayed on a WAP phone, news website, headlines news ticker, news e-mail, SMS message or in a piece of software, all from the same source file. As you can see, this creates a huge benefit, as a story can be written once by a journalist, but distributed around the world in many different formats. You can find more information at XMLNews.org.

Conclusion

Although XML still has a long way to go to become a mainstream programming language, it has great potential. After reading this tutorial you should know how to create a basic XML document and also how to output it in a browser. With this knowledge you will be able to create XML solutions for your website.