Learn XML Programming

Section 2: XML Syntax

Alright, I want to use XML.  What do I do first?

The first thing that you’ll need to do is open up your text editor of choice.  Place your XML directive on the first line, as mentioned earlier.  At this point, your document is going to look something like this (if you’re using XML version 1.0):

<?xml version=”1.0”?>

Once you’ve typed your directive, it’s time to start adding some content to the page.  Information on an XML page is handled in a very precise and structured format, using tags to define your data.  White space can be included in the document, to make it more easily readable… though you should be careful not to use that white space inside of your tags, as it can create problems when being read by a browser.

Let’s say that you’ve decided to create a new XML document, to tell the world about your two favorite cats.  You want to use the tag, <cats>, and let everyone know how much you love those little furballs.  Your document now looks a little something like this…

<?xml version=”1.0”?>

<cats>Tooter and Shade are the best cats in the world!</cats>

Note the white space in between the directive and the first tags.  You could also have put both of the tags on their own line, with the content of the tags between them, as long as you don’t add additional white space within the tags.

Of course, for the moment the <cats> tags don’t do anything.  If you load this page into a web browser, you’ll end up with more or less a copy of the file contents displayed on the screen with the tags in some pretty colors.  You’ll have to define the tags, which can be done in 1 of 4 ways:

  • Using Cascading Style Sheets (CSS)
  • Using the eXtensible Style Language (XSL) Style Sheets
  • Using a Data Island plus Script
  • Using a Data Object Model plus Script or Client-Side Program

All of this might sound complicated, but it’s really not.  It does involve creating and referencing other pages, though… and for now we’re still working on just the basic structure of XML.  Save your document (in Text-Only mode) under the name cats.xml (making sure to use the .xml extension) and we’ll get back to the <cats> tags in a moment.

How do I structure my XML documents?

Structure in an XML document is very important.  Small errors in the structure of your document can have large effects on the overall outcome; pieces may not be displayed correctly, or might not appear at all.  If the structure is too damaged, then the entire document might fail to work.

So what is the proper method of structuring an XML document?

As previously mentioned, all XML documents begin with the XML directive.  Open up the previously-saved file, cats.xml, and you’ll find your directive already in place.

<?xml version=”1.0”?>

<cats>Tooter and Shade are the best cats in the world!</cats>

Unfortunately, your file is still missing a few vital elements.  The <cats> tags don’t work, and the browser has no idea how to make them work.  If you load it up in a browser, you’ll just see a copy of the file, with the various elements in different colors.  This is actually useful, however; as long as you see this, then your code is good.  The browser doesn’t know what else to do with it, in this case because some of the elements are missing, but the lack of definitive error codes tells you that it’s at least well-coded.

Go into the file, between your directive and the content, and get ready to add another vital element to your page.  Type the following:

<?xml-stylesheet type=”text/css” href=”cats.css”?>

Of course, this doesn’t mean much to you right now… in time, though, it’s going to be a vital part of your page.  What you just typed is the directions that the browser needs to find the XML processor, or the file that tells it how it should handle the information in the XML document.  The line that you just typed tells the browser to find the file called cats.css, and that the file is a Cascading Style Sheet.  It also tells it that it’s the stylesheet that it needs for this page.

Now your cats.xml file should look like the following, which looks a lot more like an XML file.

<?xml version=”1.0”?>

<?xml-stylesheet type=”text/css” href=”cats.css”?>

<cats>Tooter and Shade are the best cats in the world!</cats>

Of course, you’ve still got to define cats.css, but that can wait.  You still have to learn a little bit more about XML first… namely the use of elements and attributes.  Go ahead and save your cats.xml file, and then we can look at what elements and attributes are.

Leave a Reply

Your email address will not be published. Required fields are marked *