How do I validate xml against a DTD file in Python How do I validate xml against a DTD file in Python xml xml

How do I validate xml against a DTD file in Python


Another good option is lxml's validation which I find quite pleasant to use.

A simple example taken from the lxml site:

from StringIO import StringIOfrom lxml import etreedtd = etree.DTD(StringIO("""<!ELEMENT foo EMPTY>"""))root = etree.XML("<foo/>")print(dtd.validate(root))# Trueroot = etree.XML("<foo>bar</foo>")print(dtd.validate(root))# Falseprint(dtd.error_log.filter_from_errors())# <string>:1:0:ERROR:VALID:DTD_NOT_EMPTY: Element foo was declared EMPTY this one has content


from the examples directory in the libxml2 python bindings:

#!/usr/bin/python -uimport libxml2import sys# Memory debug specificlibxml2.debugMemory(1)dtd="""<!ELEMENT foo EMPTY>"""instance="""<?xml version="1.0"?><foo></foo>"""dtd = libxml2.parseDTD(None, 'test.dtd')ctxt = libxml2.newValidCtxt()doc = libxml2.parseDoc(instance)ret = doc.validateDtd(ctxt, dtd)if ret != 1:    print "error doing DTD validation"    sys.exit(1)doc.freeDoc()dtd.freeDtd()del dtddel ctxt