Spring 2005 CSCI 173 lab 7

Today we're going to look at XML, Extentensible Mark Language. Much of this lab will be written on the whiteboard as needed.

An XML example

An XML format is described by a DTD, Document Type Definition. Take a minute to study the Real Estate Transaction Specification (RETS) DTD, Pay attention to the DTD elements Basement and Baths.

Writing a XML document

Now we're going to write an XML document, without an associated DTD. This might seem like putting the cart before the horse; but because DTD's are so hard to generate, it's a common practice. We're going to do this using a program called NetBeans, which is also used in CSCI 201 to write Java programs.

However, before you start NetBeans we want you to copy a few example files to use in the lab. The following commands will copy those files for you and will then start NetBeans.

[yourid@yourmach yourid] unzip /home/brock/173/XML173.zip
[yourid@yourmach yourid] netbeans &

It will take NetBeans a while to start up. When it's running, open the project /home/yourid/XML173b. You may need to ask one of the veterans of CSCI 201 how to do this. You'll probably have an even longer wait while NetBeans opens the project.

Now you need to open a new file. The menu sequence for doing this is File → New File... or you can type Cntl+N

Be careful now. In the following New File window you must choose XML from Categories and choose XML Document from File Types.

Next choose any file name you wish, but select Well-formed Document as your document type. You should soon see a window for typing XML.

Now start making up an XML file to encode your class schedule. Here's how it might start:

<schedule>
 <class><department>CSCI</department><number>173</number>
<schedule>

There are many things to keep in mind. For example, in XML, start tags must be matched with end tags, so that above example needs a </class>. Also, keep in mind that a class may have several different meeting times. For example, this class meets at different times and in different locations during the week. At the least try to encode the meeting times for this class. However, you should also add one more of your classes.

Rather tedious, isn't it.

Now right-click on the name of your XML file and select Check XML from the menu. Expect to have lots of problems. You'll probably need to fix your XML many times before it will pass the check.

Now the fun part. Right-click on your XML file name, but this time select Geneate DTD.... You'll have to give a name for your DTD.

Probably your DTD is not as restricted as it should be. For example, mine has the definition:

<!ELEMENT class (meeting|number|department)*>

However, what I really want is something like:

<!ELEMENT class (department,number,meeting*)*>

Spend a little time refining your DTD. As you go so, frequently right-click on the name of your DTD file and select Check DTD.

So, how do you make sure that your XML and DTD file now match? Well, go back to your XML file and add something like the following as the second line of your XML file.

<!DOCTYPE schedule SYSTEM "newXMLWizard.dtd">

This time when right-click on the name of your XML file, choose Validate XML. When you check an XML file, you do little more than make sure it have matching start and end tags. When you validate an XML file, you make sure that it follows a DTD (or XML schema) specification.

Adding attributes

Let's do one more thing. At UNCA, some foreign language courses have "sessions" which are indicated with either a F or L in the schedule. Let's add these are attributes, so you'd have to say something like <session term="F"> in your schedule for one of these.

Let's edit the DTD to allow this type of attribute. Review the Real Estate Transaction Specification (RETS) DTD if you want to see how this might be done.

Now modify your DTD and XML to allow a session attribute. If you want the session attribute to be optional, you'll need to put #IMPLIED within your ATTLIST. If you get odd error messages when validating your XML, you should use the menu sequence File → Save All and try again.

Displaying XML in a browser

You can right-click on the name of your DTD file and select Geneate CSS.... This gives you a rather boring CSS file. In order to use this CSS file, you must place something like the following into your XML file as the second line.

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

Now let's change the CSS. At the least, you'll want to change the line that looks something like:

class { display: block }

Make it look a bit more like this:

class { display: block ; color : green }

Then you should be able to go to your browser and load the XML file.

Is there a better way?

There are programming tools that automate the process quite a bit more; but XML is for programs, not people.