B336 title
Murdoch University logo

Laboratory Week 6 - XML Documents


Learning Objectives:

  1. Learn how to create well-formed and valid XML documents.
  2. Learn how to verify that an XML document is well-formed and valid.
  3. Understand how web servers and a web clients deal with XML documents.

Required Reading:

Lectures: Week 5 lectures
Sybex Textbook XML Complete Chapters 1-4, 6.

Access to Software:

The following exercises requires a browser on your local machine, as well as ssh access to gryphon.murdoch.edu.au.

Access to required files:

The following exercises make use of XML files and Perl scripts available in the unit coordinator's directory (~hiew/examples/xml/) on gryphon. Use the command "cp" to copy the file over to your work directory on gryphon. Eg.

gryphon:~> mkdir ~/labwk6
gryphon:~> cp ~hiew/examples/xml/* ~/labwk6

Instructions:

  1. Use a text editor to open the file course-notwf.xml. It is an XML document that is not well-formed. It contains one simple mistake. Can you spot the mistake? Regardless of whether you can spot the mistake or not, DO NOT correct the mistake for now.
  2. Use the script checkwf.pl to check if the XML document is well-formed. Type the command:

    gryphon:~/labwk4> perl checkwf.pl course-notwf.xml
    syntax error at line 3, column 0, ...


    This indicates that the script has detected a syntax error at column 0 line 3 of course-notwf.xml. Is that the same mistake as you picked in (1) above?

    Correct the mistake in course-notwf.xml by deleting the problem text. Save the corrected file as course-wf.xml. Try checking the document again with checkwf.pl to confirm you have made the right correction.

    You may use a text editor to view the source code for checkwf.pl, but your are not required to understand the code for now. We will deal with writing XML parsers in future lectures and labs.
  3. Now try the XML document course-notvalid.xml containing a DTD. This document also contains a mistake. Repeat the same steps in (1) and (2) with this document, except use the script checkvalid.pl instead of checkwf.pl. Save the corrected XML file as course-valid.xml.
  4. (For students with Micorsoft Internet Explorer 5+) Start up IE5+ and load course-valid.xml using File>Open. IE5 has an in-built XML parser. 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.

    Use IE5 to load course-notwf.xml. You should see that it detects the error and displays where it believes the error is.

    IE5 has a parser called MSXML, which is a non-validating parser (ie. it does not validate the elements against the given DTD). Use IE5 to load course-notvalid.xml. You should see that it does not detect the validity error.
  5. Try using the browsers you have available on your local machines to load the XML document. Most browsers that do have in-built XML parsers (eg Netscape 6+) will not display the content in fancy ways like IE5. Some that do not have any in-built support for XML will only display it as a normal text file, with the tags appearing as well. You will learn in future labs how to prepare the XML file so that the display is more appropriate.
  6. Now that we have seen how your browsers can deal with XML files, let's try getting web servers to serve XML files. First, try it with your Apache web server. Use seccure FTP to upload course-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:
    1. Use getall.pl from week 4's lab to confirm that the "Content-type" header field contains the value "text/xml".
    2. Use your own browsers to request the same URL as (i). You should see the same display as in (4) and (5).

  7. Stop your Apache server. Copy course-valid.xml to the same directory as your web_serial.pl and Web.pm, and start your web_serial.pl with your port number. Now do the same as (7). Note the following:
    1. When using getall.pl, have a look at the "Content-type" header field. It should be "text/plain". This is because the default type set the lookup_file() subroutine in Web.pm is "text/plain".
    2. When using any browser besides IE5 to request the URL, you should see the file content displayed as plain text. This is because it received "text/plain" as the MIME-type for this file.
    3. When using IE5 to request the same URL, the file gets displayed in the same interactive tree format as in (4). This is in spite of the "Content-type" header stating that the file is "text/plain". In this case, IE5 have determined on its own that the file is really an XML file, and therefore parses it and displays it as it sees fit. Such is the philosophy of Microsoft.
  8. Besides using the perls scripts above and using your browsers, 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.
    In these services, you have a choice of uploading your local XML file, cutting-and-pasting the XML to a form, or specifying a URL where your XML file is. Use any of these methods to try these services on course-notwf.xml and course-notvalid.xml.
  9. Student using MS-Windows may try using XML Notepad, available for download at the unit downloads page. XML Notepad has a graphical user interface that prompts you for well-formedness or validity errors, and allows you to enter XML elements in a tree-like manner. You may use it if you find it useful, but for the sake of this course, you will find using a text editor a lot faster (but less graphical) in dealing with your documents.

    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.

Exercises:

  1. Using a text editor, add the following information on a unit into course-valid.xml file. You must determine the appropriate elements to put them into.

    Unit name: B102 Introduction to Computer Science
    Lecturers' names: Peter Cole and Nicola Ritter
    Semesters: 1 and 2

  2. Put the DTD section of the course-valid.xml into a file called course.dtd. Modify course-valid.xml so that it refers to this external DTD instead of using an internal DTD.

  3. Look at the source code in mistakes.xml. This document contains multiple mistakes according to the rules of well-formedness and validity. Try and find them manually without using the software above. Correct the mistakes, and then verify that you have found them all using the software described above.

    You do not need to understand the content or the meaning of any of the tags in the document. The point of your exercises now is to determine how well you can deal with structure alone.

  4. Add to your course-valid.xml document.some of the extra information available on B336 and B108 from the School of IT unit information page.
  5. Look at the University Handbook entry for BSc in Internet Computing at Murdoch University. Consider all the relevant information may be added to the course-valid.xml document.

Assessable Tasks:

Exercises 1, 2 and 3.

Internal students should demonstrate to your tutor by executing your scripts with your tutor present. This must be done in week 6 or week 7. No marks will be awarded if the work is demonstrated after that - your tutor have no discretionary power on this deadline.

External students should submit the scripts and sample runs as Assignment 3.

 

H.L. Hiew
Unit Coordinator


Document author: H.L. Hiew, Unit Coordinator
Last Modified: Monday, 31-Mar-2003 17:12:37 MST
Disclaimer & Copyright Notice © 2003 Murdoch University
This document is relevant for semester 1, 2003 only