Exercise

Which of these XML statements are well-formed?

1) <class></class>

2) <class><lecture></lecture></lecture> <lecture></class>

3) <class><lecture></lecture><lecture/> <practical /></class>

4) <lecture> <slide Nr="1"/> <image><jpg/></image> </lecture> <practical><slide Nr="2" size="12" > </slide> </practical>

5) <class><lecture>First lesson with <exercise> One</exercise> </lecture></class>

6) <class> This is true:5 < 6, isn't it? </class>

7) <class><lecture>This is a bracket: &#60; </lecture></class>

8) <html><img src="file.gif"></html>

9) <html><font color=blue>Hello</font></html>

Working with XML

It is best to edit XML files with a dedicated XML editor in order to check the XML syntax and automatically generate DTDs and Schemas. A free, open-source XML editor is Butterfly. Another free (PC-only) XML editor is XmlWrench which also has an online interface. Browsers, such as Firefox, can be used to check whether an XML file is well-formed. There are also Firefox add-ons for XML development.

1.1 Exercises

1) Install an XML editor. The exercises below assume that you are installing the Butterfly editor, but other XML editors should have similar functionality. To run the Butterfly editor on Unix type "sh run.sh" in the Butterfly folder; on Windows click on run.bat or ButterflyXML.exe.

2) Click on "Create a new XML file". Create an XML file for a weekly schedule with

Note: Butterfly does not have a copy/paste function, but Ctrl-C/Ctrl-V might work. Otherwise, once you have entered the information for one day, you can right click on the tags and use "add sibling/add child" to enter the tags for other days, etc.

Think about the best modelling strategy for this XML file. Should Monday, Tuesday, etc be elements, attributes or PCDATA (ie content of elements)?

3) When you are finished entering the data, right click on a tag and then select "Generate DTD". Modify the DTD so that events are optional and descriptions of elements are optional. (Note: DTDs use a regular expression like notation: * means 0 or more, + means 1 or more, ? means 0 or 1).

4) The following XML is a mess! Save it as a file and open it in your XML editor. Fix all the problems. Note: don't try to generate the DTD while there are errors in your XML because Butterfly may crash. Save the XML before creating a DTD. Create a DTD and a Schema.

<!- This is about movies! ->
  <!ELEMENT actor ( #PCDATA )>
  <!ELEMENT movie (actor*, title, year*)>
  <!ELEMENT title ( #PCDATA )>
  <!ELEMENT year ( #PCDATA )>

  <movie id = 1>
    <title>The Quest</title>
    <actor>Tom Smith</actor>
    <year>2007</year>
  <movie id = 2>
    <title>Summer</title>
    <year>1998</year>
    <actor>Susie Black</actor>
    <actor>Paul White</actor>
  </movie>
 <movie id = 3><title>Hello World</movie></title>
5) The jQuery.xmleditor is an XML editor fully written in Javascript which can be incorporated into websites. The full code is available at https://github.com/UNC-Libraries. A demo of the editor has been prepared for this exercise. Download the demo.zip file and unpack it in a director on your server. Also, download the jQueryXmleditorDemo.html file. Use the xsd schema for the movie example from the previous exercise and store it in a file name "movies.xsd". In the jQueryXmleditorDemo.html file make sure that the rootElement is set to the root element in your movies.xml file (i.e. if your root is not "movies" you need to change that value).

Test whether the jQuery.xmleditor works. Some options will only work in some browsers. Try changing between text and xml. Add some subelements and attributes. Save an example of the movies.xml file in the same directory and uncomment the line with ajaxOptions. Then reload the page.

How would this work with in combination with a PHP script? Write a PHP script that reads an xml file from the current directory and presents it using the jQuery.xmleditor.