Validate an XML Document with a DTD
The Document Type Definition (DTD) is native to XML 1.0. You’ll learn how to use DTDs in this hack.
XML inherited the Document Type Definition (DTD) from SGML. It is the native language for validating XML—though it is not itself in XML syntax—and is interwoven into the XML 1.0 specification (http://www.w3.org/TR/2004/REC-xml-20040204/). Using non-XML syntax, a DTD defines the structure or content model of a valid XML instance. A DTD can define elements, attributes, entities, and notations, and can contain comments (just like XML comments), conditional sections, and a structure unique to DTDs called parameter entities. DTDs can be internal or external to an XML document, or both. This hack shows you how to implement all the basic structures of a DTD.
Example 5-1 shows external.xml , and Example 5-2 shows a DTD against which external.xml is valid. The external DTD is called order.dtd . This is also known as an external subset . This DTD is a local file in this example, but it could also exist across a network.
Example 5-1. external.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE order SYSTEM "order.dtd"> <order id="TDI-983857"> <store>Prineville</store> <product>feed-grade whole oats</product> <package>sack</package> <weight std="lbs.">50</weight> <quantity>23</quantity> <price cur="USD"> <high>5.99</high> <regular>4.99</regular> <discount>3.99</discount> </price> <ship>the back of Tom's pickup</ship> </order>
External Subset ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access