navigation
 
A Friendly Introduction to XML

I am certain that you have heard about XML. All sorts of legends have already sprouted up about this technology... like that it is supposed to replace HTML, only the most advanced webmasters can use it, it will prevent tooth decay, etc. I can assure you that none of these things are true.

Here are the basic facts that you need to know to use XML:

1. XML is a mark up language much like HTML.

2. XML must be "well formed".

3. XML must have a style sheet associated with it.

That's it! There are a few more details about each of these items, but those are the basic things that you need to know.

Before we get too deep, let's take a look at what XML looks like. As you know by now it is traditional to begin using any new code or scripting language by having it print out "Hello World!". This is how it might look in XML:

*Keep in mind that you must save you document as an XML file (myfile.xml instead of myfile.htm).

<?xml version="1.0"?>

<?xml-stylesheet type="text/css" href="mystyle.css"?>

<GREETING>

Hello World!

</GREETING>

OK, that may look a little odd, but really it is very much like plain 'ol HTML. As a matter of fact every part of this example has an HTML counter part. Let's go over each line and see how the three basic rules of XML apply to them. First, as you have already noticed, it is a mark up language like HTML. The very first line is the XML version of the <HTML> tag. It tells the browser that the document is in XML. In reality, according to the W3, whenever you create an HTML document you are supposed to make a tag much like this, but HTML is forgiving enough that your page will work fine without it. The only major difference is that you don't have to include a closing tag when using XML.

Now look at the second line. Obviously this is the XML version of a "linked" style sheet. (Remember rule #3? XML must have a style sheet associated with it.) All that we have left to explain is rule #2: XML must be "well formed". A way to think of "well formed" is that it must use proper grammar and punctuation. HTML can be very sloppy. In HTML you can leave off some closing tags and you don't have to put quotation marks around attribute values (e.g., type="text/css") and it will still work. Not so with XML. If you get sloppy with XML it won't work at all. Just make certain when using XML that you type everything the correct way and all will turn out well. (If you don't, most web development platforms like Dreamweaver have an XML "checker" that will run through your code just like Internet Explorer does for JavaScript. It will be easy to spot improper XML with this tool.)

So far, so good! But what about those <GREETING> tags? Where did they come from? Well, one of the really cool things about XML is that you can define your own tags. I didn't have to use <GREETING> . I could have made up anything that I wanted to use ( <smellydog> , <fungus> , <soretoe> , etc.). This is because each tag that you use is defined in the stylesheet. Here is an example of a stylesheet that I could create for our "Hello World!" exercise:

GREETING {display: block; font-size: 24pt; font-color: red}

This is nothing more than a typical CSS document. After I saved the file as "mystyle.css" and accessed the original XML file with a browser, I would get something that looked like this:

Hello World!

OK, "Big deal!", you might say, "So what good is XML? Isn't it more hassle than plain HTML?" Where XML really shines is when you are dealing with large amounts of data that you want arranged in a web page. Let's say that for some weird reason that I wanted to make a chart of all of the cars that I have owned. I would make an XML document that would look like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="carstyle.css"?>
<t>

<row><title>Cars that I have owned:</title></row>
<row><car>Oldsmobiles</car></row>
<row><model>Cutlass</model>
<model>Delta 88</model></row>
<row><car>Nissans</car></row>
<row><model>Sentra</model>
<model>Pathfinder</model></row>
<row><car>Fords</car></row>
<row><model>Thunderbird</model>
<model>Capri</model></row>
<row><car>Other Makers</car></row>
<row><model>Vega</model>
<model>La Saber</model></row>
<row><model>Pup</model>
<model>Grand Am</model></row>
</t>

Then I could use a style sheet that looked like this:

title {display: table-cell; font-size: 18pt; color: red; font-weight: bold}
t {display: table; width: 200}
row {display: table-row-group; width: 200}
car {display: table-cell; width: 200; color: #FFFFFF; background-color: #666666}
model {display: table-cell; text-align: center; color: black; width: 98; background-color: white}

When viewed in a browser my XML page would look like this:

Cars that I have owned:
Oldsmobiles
Cutlass Delta 88
Nissans
Sentra Pathfinder
Fords
Thunderbird Capri
Other Makers
Vega La Saber
Pup Grand Am

I have to admit that for a database this small I might do better with HTML, but what if I had hundreds or even thousands of entries? It would be pretty stressful to handle all of the information in a web page. Or what if I wanted to add or delete data? I might be forced to change the entire document. With XML, handling all of this data would be relatively easy. What makes XML even better to use for something like this is that after I have defined the style sheet for a specific tag, everything that I add using that tag will show up in the browser exactly as it is supposed to.

Even better than this, really cool things begin to happen with XML when we take it beyond web pages. The same XML document that I used to create my automobile table could also be used as WML ( web pages for pagers and cell phones), printed documents (a brochure or a book), phone/voice systems ("If you would like to hear about the Nissans that James has owned, press two."), and a myriad of other applications. Using an XML document for these different forms of presentation is as simple as using a different style sheet for each one. I would not have to do any other programing or altering of the data ... just make a different style sheet for each application.

If I needed to, I could even go another step and work out a system that would only pull out any specific information that I wanted from an XML document. For example, say that my web page was set up so that you could check to see if a specific car was on the list. I could write a script that would search through the XML tags for the type of car that you were looking for and only print out that entry to a web page..

As you can see, the flexibility of XML opens up a large number of possibilities for its use. So far the surface has not even been scratched. One of the areas that XML is quickly expanding into is that of professional document standardization. Eventually, nearly every occupation will have an XML application that defines the tags and style sheets for documents used in that profession. The following is a partial list of some of the more prominent ones that have already been developed:

Abbreviation Stands for Application used for
CML Chemical Markup Lang. chemistry
VML Vector Markup Lang. vector graphics
GedML Ged Markup Lang. genealogy
MML Mathematical Markup Lang. mathematics
HR-XML Human Resources-XML human resources
MusicML Music Markup Lang. music
VoiceML Voice Markup Lang. voice mail and phone systems
OFX Open Financial Exchange consumer level financial transactions

Regardless of your chosen career field, I would suggest that you start studying XML. No matter what area that you go into, XML will eventually become an integral part of what you do. It will be better for you to be ahead of the game than trying to catch up.