Generating a DTD the Easy Way
April 12, 1999
Remember I said not to worry about the fact that we hadn't given
any thought to a DTD? Fortunately, there is an online service
called DTDGenerator FrontEnd that will generate a DTD for you
from your XML instance! It's incredibly easy to use and does a
very good job.
Let's use DTDGenerator FrontEnd to generate our Collection DTD.
Go to the above web site and press the Browse...
button, and locate your local copy of the file
collection1.xml which we used earlier.
Press the Generate DTD! button. The generated DTD
will appear in a new browser window. Use the browser's
Edit|Select All choice and paste the DTD into a new
text file. Alternatively, use the browser's Save As
feature. The result should be identical (except for the first
line or so) to the DTD collection1.dtd included
in the earlier ZIP download.
So, why would anyone write a DTD by hand if this service exists?
Well, first of all, a single XML instance does not necessarily
represent the complete DTD any more than a single HTML file
uses every possible element and attribute of HTML. For example,
our collection1.xml example does not include
Record, Poster,
FlickerButton, or DrinkingCup
elements that would be found in any true Beatles fan collection.
They should be in the language even if they're not in my
collection. Another reason (related to the first) is that there
are many subtleties that might not be revealed by a single
document instance, such as order of element nesting
(content model), whether elements or attributes are optional,
whether Book and CD elements
need to appear in a certain order with respect to each other, etc.
Our example was purposely constructed to show that
Rating and Note elements for
Book elements were optional, that the country
attribute of the Peak element is optional, and that
there can be more than one Remastered element for
a CD (since CDs are often remastered more than
once so avid collectors will buy multiple copies). Look at the
generated DTD (excerpted below) to verify these details were
detected by DTDGenerator.
<!ELEMENT Book ( Title, Author,
Type, Published, Rating?, Notes? ) >
<!ELEMENT CD ( Title, Artist, Chart,
Type, Label+, AlbumReleased, Remastered+ ) >
<!ELEMENT Chart ( Peak+ ) >
<!ATTLIST Peak country NMTOKEN #IMPLIED >
The "?" after Rating and
Notes confirms that these are optional elements; question
mark means zero or one occurrence. The word
"#IMPLIED" for the country attribute of
the Peak element means that the attribute is
optional. 3.
The "+" after Remastered
means one or more instances of this element are allowed (the
same is true for Label and Peak).
This section is not meant to cover DTD syntax completely. See
Selena's DTD tutorial
or my
Syntax Overview
(which needs a little updating, but it is otherwise a good start).
3Actually, my intention was
that if country is not specified, the default value should be
"US". This is another example of something that would need to be
added to the generated DTD.
Viewing It With IE5, Take 1
Doing It With XML, Part 1
Adding the DTD to Our ML
|