XML/DTD Laboratory Session
The purpose of this laboratory session is to help you learn about
XML and DTDs. To do so, you will be asked to use a text editor to
create XML files representing information about two different
applications, along with an appropriate DTD in each case.
You will also be asked to check whether or not your XML representation
is valid with respect to the DTD using an online validator.
The information to be represented in each case is as follows.
-
Consider an application in which the stock of new and used
cars held by a car dealer is to be represented in XML.
The
stock
of the dealer comprises zero or more
cars, each of which is either a
new-car
or a used-car
. For a
new-car
, information on model
and
price
are recorded. For a used-car
,
information on model
, mileage
and
price
are recorded, along with optional information
about the condition
of the car.
Elements model
, mileage
, price
and condition
contain text.
You should not include any currency symbols in the contents
of price
elements.
-
Consider an application in which the results of football games
are to be represented in XML. (The description below is deliberately
more open-ended than the one above.) For each game, we want to be
able to represent the two teams involved, which one was playing at home
and which one away,
which players scored goals (some of which may have been
penalties) and the time when each was scored, and which players were shown
yellow or red cards (and also the times of these). You might use some attributes this time, for practice.
Unfortunately, the English
Premiership results, for example, no longer give this much detail (you do get almost as much
if you click on the link "Show scorers").
The data is also available at FlashScores,
but only if you click on each match and view the summary (which includes substitutions as well).
Alternatively, you can use sample data for two games
from a number of seasons back.
In each case above, you should follow the following steps.
- Create the XML file:
-
Choose a directory in which you will create the XML file, which should
have an extension of
.xml
.
-
Use a text editor, such as Textpad or Notepad++, to enter information in XML
which conforms to the specification of the application
(do not worry about the DTD yet). For the first example, you should
represent all the information using elements rather than
attributes (for the second, you are free to use attributes too).
You should include enough sample data to
demonstrate each of different structures which are allowed by the specification.
Remember to include the XML declaration
at the beginning of the file.
- Check that the XML file is well-formed:
-
Open the file you created in (1) using a browser to check that
it is well-formed (the browser will give an error if it is not).
-
Modify the file until it is well-formed.
- Create the DTD:
- Now modify the XML file created in (1) to include an
internal DTD
for documents of the type you have designed,
trying to ensure that the XML representation
you created is valid with respect to this DTD.
-
Validate the XML file:
-
Go to the online XML
Validation Service. You can upload your file to the service for
validation. The results of the validation will be presented on a separate page.
(Don't worry if you receive a warning about a missing
charset
attribute.)
-
Modify your XML file until it is valid.
-
Invalidate the XML file:
-
If you produced a valid XML document straight away or only had to correct
a couple of mistakes, now modify your file in ways that you believe make
it invalid and check that you understand the error messages returned by
the validation service.