CIT041X Index > Assignment: DTD > Hints

Hints for Assignment
Creating and Using a Simple DTD

Hint one:

Don’t try to do everything at once!

Hint two:

You can do some of the second part of the assignment (Using the DTD) first. Since you have an English language description of what makes a document valid, you can take the well-formed (but not valid) XML file and make it fit the specifications. You can do that part of the assignment before you write the DTD. Remember, the DTD just tells the validator what’s correct. The English language description tells you what’s correct.

Hint three:

Build up your DTD in parts.

Let’s take the following markup language for real estate:

Specification of the Language

Start with the first definition:

The root element of a document is the <properties> element. It consists of a <street> element followed by one or more <lot> elements.

We’ll make that portion of the DTD.

<!ELEMENT properties (street, lot+)>

We cannot use the DTD as it stands, since it refers to the (as yet undefined) <street> and <lot> elements. So we will add “placeholder” definitions, defining them both as containing plain text. We save this DTD as properties.dtd.

<!ELEMENT properties (street, lot+)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT lot (#PCDATA)>

Now we will create an XML file that contains only the elements that the DTD describes so far, so that we can test that part of the DTD.

<!DOCTYPE properties SYSTEM "properties.dtd">
<properties>
    <street>Crestview Drive</street>
    <lot>first</lot>
    <lot>second</lot>
</properties>

Sure enough, that validates just fine. So now we go on to the next part of the specification:

The <street> element contains plain text.

A <lot> contains a <streetnumber> element followed by a lot specification (see below), followed by a <value> element.

Well, it looks like we guessed right on the <street> element—it does contain plain text, so we don’t have to change it. The <lot> element isn’t just plain text, so we have to alter its definition. There’s no such thing as a "lot specification" element, so we put in a placeholder for it:

<!ELEMENT properties (street, lot+)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT lot (streetnumber, lot-specification-goes-here, value)>

We now must look ahead to the definition of a lot specification, which is either a single <house> element, <business> element, or <vacant> element. Now we can do the replacement into the DTD:

<!ELEMENT properties (street, lot+)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT lot (streetnumber, (house | business | vacant), value)>

And now, we add “text content” placeholders for the elements that haven’t been defined yet:

<!ELEMENT properties (street, lot+)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT lot (streetnumber, (house | business | vacant), value)>
<!ELEMENT streetnumber (#PCDATA)>
<!ELEMENT house (#PCDATA)>
<!ELEMENT business (#PCDATA)>
<!ELEMENT vacant (#PCDATA)>
<!ELEMENT value (#PCDATA)>

Then we update our XML test file to reflect the elements that we have added, so that we can test this part of the DTD:

<!DOCTYPE properties SYSTEM "properties.dtd">
<properties>
    <street>Crestview Drive</street>
    <lot>
        <streetnumber>2211</streetnumber>
        <house>...</house>
        <value>100000</value>
    </lot>

    <lot>
        <streetnumber>2213</streetnumber>
        <business>...</business>
        <value>500000</value>
    </lot>
</properties>

You might also want to make an XML file that does NOT fit the specification, just to make sure that it doesn’t validate. Here’s file testbad.xml, which is trying to be both a house and a business:

<!DOCTYPE properties SYSTEM "properties.dtd">
<properties>
    <street>Crestview Drive</street>
    <lot>
        <streetnumber>2211</streetnumber>
        <house>...</house>
        <business>...</business>
        <value>100000</value>
    </lot>
</properties>

You continue in this way, adding a little bit at a time to your DTD, then adding a little bit to your XML test file, until you have finished the DTD and a sample file. Then you can go to the catalog file given in the assignment and make it fit your DTD.