Exercise 3 – Building a DTD for the résumé

In this exercise, you will develop a DTD to define the structure for the XML résumé that you created in the previous exercise. Based on the valid XML instance file that you created for the résumé, you can start developing a Document Type Definition (DTD).

The following example includes a change to the XML than what was used in the previous exercise. The sample now includes an attribute on the <school> element. The attribute identifies the kind of school, which could be a university, a college, a vocational school, or some other kind of educational institution.

<resume>
   <!-- complete content goes here -->
   <school type=”college”>
      <name>Austin Community College</name>
      <degree>Associates of Applied Science</degree>
      <year>1988</year>
      <location>
         <city>Austin</city>
         <state>Texas</state>
      </location>
   </school>
</resume>

Remember that each element within the source requires an information model within in the DTD. The following example shows information model for the <school> element. Notice it is followed by the ATTLIST declaration, which declares the new type attribute.

<!ELEMENT school (name, degree, year, location)> 
<!ATTLIST school type CDATA #REQUIRED>

The <name> element is a child of the <school> element and contains parsed character data (text; "parsed" means it has to be well-formed XML), as shown below:

<!ELEMENT name (#PCDATA)>

To create the DTD, you need to create an information model (<!ELEMENT> statement) for each element you used in your XML résumé instance file. oXygen provides a shortcut method to get started, which we will use in this exercise. Alternatively, you can select File > New… > New document > Document Type Definition to create a DTD from scratch.

Step 1: Create a basic DTD file

Open your résumé XML instance file created in the previous exercise. In oXygen, select Document > XML Document > Learn Structure. Then, select Document > XML Document > Save structure. Name the document yourName-resume-DTD.dtd.

oXygen creates the DTD file and adds a reference to it in your instance file.

The following example shows the DTD file created by oXygen based on the sample resume content file.

<!ELEMENT resume (#PCDATA | school)*> 
<!ELEMENT degree (#PCDATA)> 
<!ELEMENT school (#PCDATA | name | degree | year | location)*> 
<!ATTLIST school type CDATA #IMPLIED> 
<!ELEMENT location (#PCDATA | city | state)*> 
<!ELEMENT name (#PCDATA)> 
<!ELEMENT state (#PCDATA)> 
<!ELEMENT year (#PCDATA)> 
<!ELEMENT city (#PCDATA)>

Your XML instance file now contains a DOCTYPE statement that points to the DTD:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE resume SYSTEM "file:/C:/vm_demos/vmorrow-resume.dtd"> 
<resume>
...

Step 2: Refine the automatically generated DTD

Although oXygen provides a quick way to generate a DTD that DTD doesn’t really provide the structure that you intended.

For example, for school, you probably would like to always make sure that for each school that the school’s name, followed by the degree, year, and, finally, the location always appear and in the correct order. The definition specified in the default DTD would let you have any number of these elements, in any order, or even text within the <school> element instead of the desired structure.

To refine the information model, modify the generated DTD to provide the rules that you actually want to use to structure the information. For example, change:

<!ELEMENT school (#PCDATA | name | degree | year | location)*>

To this:

<!ELEMENT school ( name, degree, year, location )>

Now, the DTD requires that each of those components (name, degree, year, and location) be present in the instance file and specifies the sequence in which they will appear.

In your résumé, restructure and refine the entire DTD so that:

  • The elements appear in the same order as they appear in the XML instance file (for readability).
  • The <!ELEMENT statements provide the structure and rules for the document that you intended.

For our abbreviated example, the refined version of the DTD would appear as follows:

<!ELEMENT resume (school)*> 
<!ELEMENT school (name, degree, year, location)> 
<!ATTLIST school type CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)> 
<!ELEMENT degree (#PCDATA)> 
<!ELEMENT year (#PCDATA)> 
<!ELEMENT location (city, state)> 
<!ELEMENT state (#PCDATA)> 
<!ELEMENT city (#PCDATA)>

Step 3: Validate your XML source

After you finish refining your DTD, save the file (as yourName-resume-DTD.dtd). Return to your résumé source file and validate that your XML content file conforms to the structure in the DTD by selecting Document > Validate > Validate. If you get an error, review the DTD to determine where the mismatch occurred and correct any issues.

Step 4: Finish and upload your XML source files to Blackboard

Once you have a valid XML résumé and DTD file, include the following files in a zipped folder named yourName-resume-DTD.zip:

  • XML résumé instance file (yourName-resume.xml)
  • XML résumé DTD (yourName-resume-DTD.dtd)

Upload this file to the Exercises area in Blackboard.

If you have issues, post your markup and any error messages to the Blackboard Discussion Board.