Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose between DTD and XSD

Tags:

xml

xsd

dtd

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?

like image 238
ajsie Avatar asked Mar 05 '10 20:03

ajsie


People also ask

Should I use DTD or XSD?

There are many differences between DTD (Document Type Definition) and XSD (XML Schema Definition). In short, DTD provides less control on XML structure whereas XSD (XML schema) provides more control.

What is the advantage of using an XSD file over a DTD file?

Advantages of XSD over DTDDTD is not extensible. XSD is defined in XML. It does not require intermediate processing by a parser. DTD is not defined in XML.

Why is XML Schema preferred over DTD?

An XML Schema can define the data type of certain elements, and even constrain it to within specific lengths or values. This ability ensures that the data stored in the XML document is accurate. DTD lacks strong typing capabilities, and has no way of validating the content to data types.

What are XML schemas how are they better than DTD?

XML schemas are written in XML while DTD are derived from SGML syntax. XML schemas define datatypes for elements and attributes while DTD doesn't support datatypes. XML schemas allow support for namespaces while DTD does not. XML schemas define number and order of child elements, while DTD does not.


1 Answers

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)

like image 184
Mike Mooney Avatar answered Oct 07 '22 21:10

Mike Mooney