XML Tutorial 3: Creating a Valid XML Document
Working with a Document Type Definition
Additional Topics
How to use XML Spy to generate a DTD file from a XML document
XML documents can be validated using a Document Type Definition (DTD). A DTD document (or set of DTDs) can be used to:
- ensure that all required elements and attributes are presented in the document;
- prevent using undefined elements and attributes;
- enforce a specific data structure;
- specify element allowed or optional element attributes and define default their values;
- describe how the parser should access non-XML or non textual content (binary data such as images).
There can be only one DTD per XML document, however, like stylesheets, many XML documents can share a single DTD. Learn more about DTD using the online tutorial at http://www.w3schools.com/dtd/default.asp and XML.com schema tutorials (Their tutorials are very helpful).
To generate a DTD file from a XML document, open your XML document with XML Spy. You can use the Generate DTD/Schema command from the DTD/Schema menu to generate a new DTD from an XML document (or from a set of documents in the project window), as shown in Figure 1A. The generated document is shown in Figure 1B.
Figure 1A
Figure 1B
To create a DTD, you must create a document type declaration in the XML document. A DTD can be declared inline in your XML document, or as an external reference (an internal subset and an external subset). External references are far more practical and scalable. XML Spy provides the means necessary for efficient editing of DTD through its enhanced grid view which is ideally suited for showing the structure of the content model. You can actually edit this model directly within the hierarchical representation. You can read how to use XML Spy to generate a DTD file from a XML document by visiting the XML Spy Web site . XML Spy runs validation tests against the rules specified in the DTD whenever you open an XML document.