ProblemUp to now, I have simply provided XML and asserted that it is valid. Now you want to verify the structure using a Document Type Definition (DTD). SolutionWrite the DTD and refer to it in one or more XML documents. DiscussionThis is not the place for a full dissertation on Document Type Definition syntax. Briefly, a DTD is a means of restricting the structure of an XML document by listing all the elements allowed, where they are permitted, and what attributes they have, if any. The DTD uses a special syntax inherited from SGML. Example 21-8 is people.dtd, a DTD for the people.xml file shown earlier in this chapter. Example 21-8. people.dtd<!ELEMENT people (person)*> <!ELEMENT person (name, email, country)> <!ELEMENT name (#PCDATA)> <!ATTLIST email type CDATA #IMPLIED> <!ELEMENT email (#PCDATA)> <!ELEMENT country (#PCDATA)> To verify that a file conforms to a DTD that is, to validate the file you do two things:
Now any elements found in the document that are not valid according to the DTD result in an exception being thrown. See AlsoDocument Type Definitions are simpler to write than XML Schemas. In some parts of the industry, people seem to be going on the assumption that XML Schemas will completely replace DTDs. But many other developers continue to use DTDs. There are other options for constraining structure and data types, including RelaxNG (an ISO standard). |