How to choose between DTD and XSD
Solution 1:
It's probably important to learn DTDs as a separate exercise, just for the knowledge of how they work in case you encounter them somewhere else, and so that you can appreciate some of the things that XSD was trying to solve.
However, for your current purposes of describing an XML document, indeed stick to XSDs.
In addition to having a far richer feature set (like you mention, including data types and namespaces), they are also XML documents themselves, which can be really useful. Because they are XML, you can check their well-formedness and validity a lot easier, and you can write code that works with them like regular XML files (for instance, if you wanted to autogenerate code classes from a schema)
Solution 2:
It really depends on how complicated the structure is that you need to setup.
If you need things like namespacing and datatypes, definitely go with XSD. If you just need a quick little schema to check against, DTD will give you faster performance since there is no XML parsing involved.
As I understand it, XSD is derived from DTD so understanding DTD will give a solid foundation for learning XSD, plus point out some of DTD's short comings.
Solution 3:
It wouldn't hurt to understand the structure of a DTD (it'll help you better understand an XSD in the long run)...but you should use XSDs moving forward.
Solution 4:
No harm in learning DTD, but be sure to use XSD, because XSD has more strength,
With XSD you can not only validate the structure/hierarchy of the XML tags but also,
- You can define Data type of the values of the nodes. [date, number, string etc]
- You can also define custom data_types, [example, for node , the possible data can be one of the 12 months.. so you need to define all the 12 months in a new data type writing all the 12 month names as enumeration values .. validation shows error if the input XML contains any-other value than these 12 values .. ]
- You can put the restriction on the occurrence of the elements, using minOccurs and maxOccurs, the default values are 1 and 1.
.. and many more ...
There are some restrictions: as like,
- An element(name) defined in XSD file must be defined with only one data-type.
- You can't validate a node/attribute using the value of another node/attribute.
Solution 5:
There is an IMHO very important issue to use a DTD (maybe together with a XSD if you need in-deep-validation):
In DTD you can define your own entities eg:
<!ENTITY MyName "DrDr.Hannibal Xerxes Utah,MBA and CEO">
In your document you can wherevever needed simply code &MyName; instead typing all this stuff.
Furthermore assume you have a XML-like file (maybe produced by some other application) that consists of a lot of similar tags but no root-tag, eg:
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- you need this when using foreign characters like 'ü' -->
<Book Author="Author1">
<Titel>Erstes Buch</Titel>
</Book>
...
<Book Author="Author5">
<Titel>Fünftes Buch</Titel>
</Book>
Assume this file is named "Booklist.TXT",
Now you can code your master-xml:
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- you need this when using foreign characters like 'ü' -->
<DOCTYPE MyRoot [
<ENTITY AllBooks SYSTEM "Booklist.TXT">
]
<MyRoot>
... some prefix-stuff as needed ...
&AllBooks; <!-- here are all the Books -->
... some post stuff es needed ...
</MyBook>
and whenever you need the books in another context you only must code the surrounding xml and habe not to touch or copy the booklist itself, furthermore you can maintenance it in one single place and have all changes in any document.