Laboratory Week 6
Learning Objective:
- Learn how to create well-formed and valid XML documents.
- Learn how to verify that an XML document is well-formed and valid.
- Understand how web servers and a web clients deal with XML documents.
Required Reading:
Week 5 lecture 1 - The XML document format
Week 5 lecture 2-1 - The Document Type Definition (DTD)Access to Software:
All required software is available in lab SC2.31.
External students may download the required software and install it on your own machine. Internal students may do the same, but are encouraged to still attend labs for supervision when doing the exercises below.
Instructions and Exercises:
In the following instructions and exercises, replace all occurences of "12345678" with your own 8-digit student number, and "15678" with "1" followed by the last 4 digits of your students number.
- Copy the XML files in the directory at this link to your local machine. Do not attempt to open the files using your browser yet - we will get to that later.
- The file poetry-notwf.xml is a document that is not well-formed. It contains one simple mistake. Using a text editor to view the document, can you spot the mistake?
- Leave the mistake in the document, and keep the text editor open. Start up XML Notepad, and open the document using File>Open. It will give you an error because it detected the mistake in the document when parsing it. Using the text editor, correct the mistake, and save the file as poetry-wf.xml. Try and open the file in XML Notepad again. It should show you the contents of the XML document in a tree structure.
- Now try the XML document poetry-notvalid.xml containing a DTD. This document also contains a mistake (in the declaration for the element poem). Repeat the same steps in (2) and (3) with this document. Save the corrected file as poetry-valid.xml.
- Using a text editor, add the following poem to the XML file:
"The Little Boy Lost" by William BlakeCheck the changes using XML Notepad.
Father, father, where are you going?
O do not walk so fast.
Speak father, speak to your little boy
Or else I shall be lost.
The night was dark, no father was there,
The child was wet with dew.
The mire was deep, and teh child did weep
And away the vapour flew.
- Learn to use XML Notepad to do the same in (5). You will find editing the elements a lot faster once you are familiar with the items in the interface. Unfortunately, it will not allow you to edit the DTD. You will still need the text editor for that. The reason for this is because since DTDs are shared, they are usually not put into the XML documents themselves.
NOTE: XML Notepad only validates the document against the DTD when the document is loaded. If you make changes to the DTD, or to the elements, and you want to validate again, you will have to load another document, then reload the first document again.
- Now load the XML document using Internet Explorer 5. IE5 has an in-built XML parser, but it is a non-validating parser (ie. it does not validate the elements against the given DTD). Once it has parsed the document, it displays it in a tree-structure similar to XML Notepad, and also add some interactivity (eg. click on the + and - symbols). Use View>Source menu item to look at the source of what it is displaying - it is exactly the content of the XML file.
- Now try the same using Netscape Navigator version 4.7 and below. Since Navigator version 4.7 and below doesn't contain any in-built support for XML, it will not know how to deal with the text/xml MIME-type.
If you have the new Netscape Navigator version 6, you may try the same with that browser. Navigator 6 does have support for text/xml, and so will display the contents (albeit very crudely) of the XML file.
- Now that we have seen that some browsers can deal with XML files, let's try getting our web servers to serve XML files. First, we will try it with our Apache web server. Use FTP to upload poetry-valid.xml to your Apache's htdocs directory. Start your server using apachectl, and use different web clients to access the file from your server:
- Use get_url_more.pl from week 4's lab to confirm that the "Content-type" header field contains the value "text/xml". Eg. use the command:
perl get_url_more.pl http://red.murdoch.edu.au:15678/poetry-valid.xml | more
- Use IE and Navigator to request the same URL as (i). You should see the same display as in (7) and (8).
- Stop your Apache server. Copy poetry-valid.xml to the same directory as your web_serial.pl and Web.pm, and start your web_serial.pl with your port number 15678. Now do the same as (9):
- Use get_url_more.pl to have a look at the "Content-type" header field. It should be "text/plain", because the default type set the lookup_file() subroutine in Web.pm is "text/plain".
- Use Netscape Navigator version 4.7 and below to request the same URL again. This time, Navigator will display the file as plain text. This is because it received "text/plain" as the MIME-type for this file.
- Use IE5 to request he same URL. In this case, instead of display the file as text, it still parses and display the XML structure. This is because like many other Microsoft products, instead of believing the "Content-type" information it receives, IE5 second-guesses what you really want and comes to the conclusion that you really want to display and XML document, and therefore parses and processes the document for you.
- Besides using XML Notepad and IE5, there are also other software which can be used to verify the well-formedness and validity of XML documents. Some of them are web based. Eg.
- XML validator from Brown University's Scholarly Technology Group : http://www.stg.brown.edu/service/xmlvalid/
- XML well-formedness checker and validator from the University of Edinburg's Language Technology Group : http://www.ltg.ed.ac.uk/~richard/xml-check.html
- XML.com's non-validating syntax checker RUWF (are you well-formed?): http://www.xml.com/pub/a/tools/ruwf/check.html
You will need to have your XML document accessible through a URL (eg. using your Apache server) to use these web services.
- (To start on Assignment 2) Think about the data and information that can be used to describe a unit like B336, which may be useful to someone considering whether to enrol in the unit or not. What set of elements would be appropriate to use. What would their structure be. Define the elements using a DTD, and come up with a few example XML files using this DTD.
- (Beware: Here Be Dragons!) Look at the source code in mistakes.xml. This document contains 10 mistakes according to the rules of well-formedness and validity. Try and find them manually. Correct them and verify that you have found them all using thew software described above.
You will be required to do the same with another XML document in the exam, and you will not have software to assist you at that time.Summary:
So now we have seen how to create XML documents, and to use them through the WWW using web servers and web browsers. Big deal! The browsers don't do anything very interesting with the information. The reason for this is that there are many different mark-up languages created using XML, and many more will be created in the future. Default browsers don't (and shouldn't) know anything about the meaning of the elements in these languages, and therefore display or process them in interesting ways. They therefore only restrict themselves to showing the structure of the documents.
To do something more interesting will require extra software. Some of the possible options are:
- Change the web server (usually by adding components or modules) so that it parses and converts the XML file to an appropriate format for general browsers (eg. HTML). Apache does this by the AddType and AddHandler directives. You also have begun to do something along the same lines with your web_serial.pl and Web.pm in Assignment 1 when you had to deal with a special .336 file type.
- Add to the web browser (usually through plug-ins for new MIME-types) so that when it receives the new XML type, it will know how to parse, process and display (if necessary) the new file type.
- Don't use the WWW and create completely new servers and/or clients to deal specifically with a new mark-up language created using XML.
- Have software to transform the documents before they get are communicated through servers and clients.
- etc...
In future labs, we will deal with the XML technologies which allows to us to easily create these software.
Document author: H.L.
Hiew, Unit Coordinator
Last Modified: Monday, 02-Apr-2001 02:29:25 MST
Disclaimer & Copyright Notice © 2001 Murdoch
University