Ever tried to read a DTD, and failed miserably? Ever wondered what all those symbols and weird language constructs meant? Well, fear not - this crash course will get you up to speed with the basics of DTD design in a hurry.
And that just about covers everything I have to say on the topic. Before I go, though, I'd like to run through a composite example illustrating everything you've learned thus far.
Take a look at the following XML document
<?xml version="1.0"?>
<!DOCTYPE review SYSTEM "movie.dtd">
<review id="42">
<header>
<title>Pearl
Harbor</title>
<cast>Ben Affleck, Josh Hartnett and
Kate Beckinsale</cast>
<director>Michael
Bay</director>
<duration
units="m">167</duration>
<genre>Drama</genre>
<slug>War
Games</slug>
<author>J. Doe</author>
<date>2001-08-08</date>
</header>
<body>
<para>On
December 7, 1941, Japan unexpectedly attacked the American naval
base
at Pearl
Harbor, hoping to gain the initiative in the war against
Europe. As
it turned
out, the attack had the effect of galvanizing the
<quote>sleeping
American
giant</quote>, resulting in the utter rout of the
Japanese and German
armies
and bolstering America's dominant role in world
politics. </para>
<para>While
<title>Pearl Harbor</title>'s love story may seem unbelievably
trite,
the
effects are most certainly not. The Japanese attack on the naval
port is
described
in tremendous detail, and is perhaps the most compelling
reason to
watch this
film. With over forty minutes of reel time devoted to
the attack,
you've probably
never seen anything like it before; it's a
visual spectacle that
hits home more
than any written description ever
will. Bay's direction is superb
- he knows just
where to put the camera,
and he always gets the money shot -
and the cinematography
and visuals -
especially those shot in the train station,
with steam billowing
out in the
background - simply gorgeous. </para>
<para>While
I think
the love story embedded within <title>Pearl
Harbor</title> isn't
really
all that compelling - <title>Moulin
Rouge</title> did it better
- this is
still a film worth watching, if only
to understand a little bit of
history! </para>
</body>
</review>
and then see if you can put together a DTD for it. Here's my version:
<!-- element declarations -->
<!ELEMENT review (header,body) >
<!ELEMENT
header (title,cast,director,duration,genre,slug,author,date) >
<!ELEMENT title
(#PCDATA) >
<!ELEMENT cast (#PCDATA) >
<!ELEMENT director (#PCDATA) >
<!ELEMENT
duration (#PCDATA) >
<!ELEMENT genre (#PCDATA) >
<!ELEMENT slug (#PCDATA)
>
<!ELEMENT author (#PCDATA) >
<!ELEMENT date (#PCDATA) >
<!ELEMENT
body
(para+) >
<!ELEMENT para (#PCDATA|quote|title)* >
<!ELEMENT quote
(#PCDATA)
>
<!-- attribute declarations -->
<!ATTLIST review id CDATA
#REQUIRED >
<!ATTLIST
duration units (m | h) "m" >
And that's about it from me. In case you're interested in finding out about the
more arcane aspects of DTDs - notations, parameter entities and overrides - you should consider checking out the following links.
If, on the other hand, all you were looking for was a working knowledge of DTDs to get you though your day, I hope you found it here. I'll be back soon with another article on a related technology, XML Schema, which is quickly gaining followers on account of its ease of use and powerful data-validation capabilities (think of it as DTDs on steroids, but without the nasty symbols). Until then, though...be good!