This tutorial will guide you through setting up Tomcat and Cocoon to serve XML pages, then you will create a DTD, XML file and three XSL stylesheets so that you can view your data in your desktop browser, a cell phone browser and a pdf file.
Your server environment is ready to go. Now we can actually start creating xml files. The first step is to create a Document Type Definition (dtd). The dtd tells the xml parser what information is allowed in the xml file and defines the format of the data. If the xml file does not fit into the constraints of the dtd the xml parser will give you an error and stop parsing the document. In our example we are creating a 'contact' element which has a name, address, phone number and email address. The dtd will define which tags must be part of a contact element and what kind of data will be in each tag.
It is not actually necessary to create a dtd for such a simple xml file, but it is important to use dtds when you are creating more sophisticated documents. Advantages of using dtds are that they ensure that your xml files are valid and well-formed and that you have all of the required data in the document. Using dtds also helps create standardization. Once you have created a dtd for a 'contact' it can be used in any place where contact information is required. This helps ensure that your data is consistent through out the company. Dtds also make it easier to communicate your data structures to others. If someone wants to interact with your applications or web site, for example, you could use dtds to show them your data structures.
To create your dtd, change directories to $TOMCAT_HOME/webapps/cocoon and create a directory called 'address'. Then change directories to address, create a file called contact.dtd and open it up in a text editor
The first step is to define you root element:
<!ELEMENT contact (name,address,phone,e-mail)>
In the line above we defined an element called contact which contains the elements name, address, phone and e-mail. Any time we use this dtd we must have a contact element which contains all of the child elements.
Next we will define the name element:
<!ELEMENT name (first-name,last-name)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT
last-name (#PCDATA)>
The first line defines the name element as containing first-name and last-name elements. The next two lines define first-name and last-name elements as text elements.
Following the same logic we can fill in the address, phone and e-mail elements.
<!ELEMENT address (street,city,state,country)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT
city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT
phone (#PCDATA)>
<!ELEMENT e-mail (#PCDATA)>
The entire dtd looks like this:
<!-- contact.dtd -->
<!ELEMENT contact (name,address,phone,e-mail)>
<!ELEMENT
name (first-name,last-name)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT last-name
(#PCDATA)>
<!ELEMENT address (street,city,state,country)>
<!ELEMENT street
(#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT
country (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT e-mail (#PCDATA)>