How to choose between DTD and XSD
Asked Answered
C

7

54

I want to use either a DTD or an XSD to describe my XML document. I've read that XSDs are better than DTDs since they support namespaces and data types, and that DTDs are older.

Does this mean that I should only use XSDs for all future needs and totally ignore DTD as an option? Should I even bother learning the structure of DTDs?

What factors should I consider when choosing between XSD and DTD?

Cohette answered 5/3, 2010 at 20:32 Comment(1)
large overlap with the vs. question: #1544700Exurbia
R
49

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)

Rebba answered 5/3, 2010 at 20:42 Comment(3)
Good short informative answer: your sentence made me wonder something: 'Because they are XML[...]you can check their [validity] a lot easier': interestingly enough the way you do that appears to rely on the DTD standard - w3.org/2001/XMLSchema.dtdEastwards
What I mean is that an XSD itself is a valid XML document, so you can open it up in and XmlDocument or whatever and parse info. DTDs are not valid XML, so if you want to write code that reads in the DTD content (like a code generator that builds XML classes), you need to parse that format yourself. I'm sure there are componets/utilities for reading DTD files, but XSDs gives you it through (relatively) easily parseable XML for free.Rebba
@monoJohnny - you're quite right that there is a DTD for XSD schema documents, but it's not quite right to say that validation of schema documents necessarily relies on DTDs; there is also an XSD schema for schema documents. The DTD was provided partly for bootstrapping (when initial drafts of XSD came out, DTDs were already widely supported) and partly for comparison purposes (to help DTD-savvy readers understand the schema for schema documents).Cankerworm
C
10

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.

Chong answered 5/3, 2010 at 20:41 Comment(0)
D
6

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.

Duhon answered 5/3, 2010 at 20:36 Comment(0)
K
5

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,

  1. You can define Data type of the values of the nodes. [date, number, string etc]
  2. 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 .. ]
  3. 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,

  1. An element(name) defined in XSD file must be defined with only one data-type.
  2. You can't validate a node/attribute using the value of another node/attribute.
Keener answered 8/3, 2010 at 11:32 Comment(4)
>>You can't validate a node/attribute using the value of another node/attribute. Does this mean I cannot have dynamic dataTypes? For example user can configure a list of database connections in a element and use the connection names in a different elements (say services) and have xsd validate if there exists a connection with the same name that is configured inside sevices element?Cutlery
@balki, Yes it is not possible. you see you can validate each element and its value seperately. You can't be dependent upon another element. like as you said in your example, you cannot check value of services elements based on value of the connection element.Keener
@balki, however you can define an enumeration list so that the allowed values for db_connection element are limited and listed. It cannot have any other value than the ones existed in list. Same thing can be done for other elements like services too.Keener
You can add restrictions to occurrences to DTD too with regex like expressions (*, +)Boor
S
3

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.

Smoothie answered 19/3, 2014 at 23:25 Comment(1)
That's a good point, but I would probably change "(maybe together with a XSD if you need in-deep-validation)" to "(maybe together with XSD if you need validation)". This is because none of your examples validate the XML. They're just entity declarations in an internal subset. If validation is needed, either an XSD would also need to be used or the full DTD would need to be supplied (either externally or in the internal subset).Swathe
M
2

This is an old string, BUT in case anyone else comes across it... from what I can tell DTD still has two benefits which XSD does not, namely the inclusion of the ENTITY function which does not exist in XSD. This is a pretty awesome feature which tells the compiler how to process potentially unfamiliar file types by identifying what programs to open to process them.

Also, DTDs are written into the XML spec so they can be written directly into XML documents whereas XSD has to exist as an outside file and connected. Not a big deal especially when using in bigger documents anyway.

I think XSD is still far better and more natural since it uses XML syntax, just wanted to play devil's advocate :)

Manichaeism answered 16/2, 2013 at 16:6 Comment(1)
it is said Here that XSD files can also be inline with xml documents , would you please explain more about the first issue? (Inclusion of the ENTITY function)Bulger
C
0

XML Schema can perform more complex validations. E.g if DTD checks the datatype of an XML element is integer or string.XML schema can perform more complicated validations like if the xml element is a string starting with uppercase letter or a positive integer. Finally XML schema uses XML syntax and its a natural choice for development of web services.

Currish answered 4/4, 2015 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.