Why is it very important to specify DTD constraints for an XML document if the latter is treated by a JavaScript file?

433 Views Asked by At

My teacher told us that an XML document which will be treated by a JavaScript program must be ruled by a Document Type Definition. He didn't explain why, and I'm wondering about it.

Indeed, DTD are not required : these constraints are just recommended for a lot of reasons. But why should we use DTD if our JavaScript program will treat the XML document ?

1

There are 1 best solutions below

0
On BEST ANSWER

Using DTD (or better XSD schema) makes sure the XML document is a valid document. This way your JavaScript code will be much simpler and will not have to take care about cases if some element is missing or contains unexpected data type.

For example, if your XML document contains <birthdate>last-year</birthdate>, your JavaScript should know what to do with this value. It is much simpler to apply validation (DTD or XSD) and make sure the document is valid.