XML technologies / XML / Python & XML / XML validation and dialects / Working with DTDs

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

Schemas and validation matter for reliable communication between applications. Understanding how schemas express document relationships is essential to using them effectively. Here we focus on DTDs, but the concepts apply to every schema language. For Python modules supporting languages other than the DTD language defined in XML 1.0, see the alternatives discussed in Document types and schemas.

A DTD appears in the internal subset, external subset or both. The internal subset resides inside the XML instance; the external subset is linked from it, telling the parser where to find the DTD.

The package xmlproc is a validating Python parser. Currently, it is the only available validating parser for Python that is also implemented in Python. If PyXML is installed, as assumed throughout this book, you already have xmlproc and can use it in programs. The package xmlproc can be imported from xml.parsers:

 >>> from xml.parsers import xmlproc 

Validating with the internal DTD subset

With some XML experience, you can probably understand basic DTD syntax from a few examples. The package xmlproc offers a command-line tool called xvcmd.py. It tests document validity against DTDs. Use xvcmd.py to try simple internal and external DTDs. Ensure xvcmd.py is on your path, normally just below your PyXML installation in xmldoc/demo/xmlproc/xvcmd.py.

Here is a small XML document named product.xml with an internal subset. For illustration, it does not faithfully implement its DTD. This may not be immediately obvious, which makes xvcmd.py useful for checking validity.

Example: product.xml with an incorrect product element

<?xml version="1.0"?>         
<!DOCTYPE product [
         <!ELEMENT name (#PCDATA)>
         <!ELEMENT price (#PCDATA)>
         <!ELEMENT product (name, price)>
]>
<product>
         <name>Bean Crusher</name>
</product>

Try the validator xvcmd.py on the command line:

C:\>python c:\python20\xmldoc\demo\xmlproc\xvcmd.py product.xml
xmlproc version 0.70

Parsing 'product.xml'
E:product.xml:9:11: Element 'product' ended, but not finished

Parse complete, 1 error(s) and 0 warning(s)

As expected, an error occurs. The DTD explicitly specifies a content model for the product element: exactly one name element and one price element:

 <!ELEMENT product (name, price)> 

The DTD also requires each of these elements (price and name) contain only character data, as these declarations show:

<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>

We can correct the XML problem shown in product.xml with an incorrect product element. The product element needs a price child, and this price element can contain only character data. Modify the document products.xml as follows, adding a price element:

<?xml version="1.0"?>       
<!DOCTYPE product [
       <!ELEMENT name (#PCDATA)>
       <!ELEMENT price (#PCDATA)>
       <!ELEMENT product (name, price)>
]>
<product>
       <name>Bean Crusher</name>
       <price>3.95</price>
</product>

Return to the command line and run the validator xvcmd.py again:

C:\>python c:\python20\xmldoc\demo\xmlproc\xvcmd.py product2.xml
xmlproc version 0.70

Parsing 'product.xml'

Parse complete, 0 error(s) and 0 warning(s)

The example product.xml with an incorrect product element now works because the instance conforms to the DTD. The DTD strictly controls the content model of ordinary XML constructs—elements, attributes and character data—permitted in every XML document.

Validating with an external DTD subset

Now consider an external subset. DTDs are normally stored externally when many instances use them, allowing one DTD to be maintained for many documents. Internal storage requires a copy in every instance, making reliable maintenance difficult for large collections. An external DTD is better there. Import it as shown next.

Example: order.xml with an external DTD

<?xml version="1.0"?>
<!DOCTYPE order SYSTEM "order.dtd">
<order>       
     <customer_name>eDonkey Enterprises</customer_name>
     <sku>343-3940938</sku>
     <qty>4</qty>
     <unit_price>39.95</unit_price>
     <product_name>eDonkey Feed Bags</product_name>
</order>

There is no internal subset. The file order.dtd contains the document type and is shown next:

Example: order.dtd

<!ELEMENT customer_name (#PCDATA)>
<!ELEMENT sku (#PCDATA)>
<!ELEMENT qty (#PCDATA)>
<!ELEMENT unit_price (#PCDATA)>
<!ELEMENT product_name (#PCDATA)>       
<!ELEMENT order (customer_name,
       sku,
       qty,
       unit_price,
       product_name)>

The next section covers exact declaration syntax. Here, only the DTD’s overall composition matters. The example order.dtd creates five elements with character-data content models. A sixth element, order, contains exactly one of the others in its content model. Every valid document using this DTD must follow that structure. Test the new document and DTD by running xvcmd.py as shown:

C:\>python c:\python20\xmldoc\demo\xmlproc\xvcmd.py order.xml
xmlproc version 0.70

Parsing 'order.xml'

Parse complete, 0 error(s) and 0 warning(s)

The document order.xml is valid. Arbitrary changes can make it invalid. Modify order.xml to look like this by removing the elements qty and product_name. This guarantees validation failure:

<?xml version="1.0"?>
<!DOCTYPE order SYSTEM "order.dtd">
<order>
  <customer_name>eDonkey Enterprises</customer_name>
  <sku>343-3940938</sku>
  <unit_price>39.95</unit_price>
</order>

The parser now complains about the structure:

C:\>python c:\python20\xmldoc\demo\xmlproc\xvcmd.py order.xml
xmlproc version 0.70

Parsing 'badorder.xml'
E:badorder.xml:6:14: Element 'unit_price' not allowed here
E:badorder.xml:7:9: Element 'order' ended, but not finished

Parse complete, 2 error(s) and 0 warning(s)

External DTD storage is generally a good idea: it is more flexible and allows many instances to be checked against one DTD. It is especially useful for documents published online. Instances can be scattered worldwide, yet remain validatable if their declarations point to a valid DTD URL. One online copy is enough.

  

 

 

 

back to top