I was wondering which of the built in python libraries, support DTD validation.
I've been looking around and haven't been able to get a straight answer. I know ElementTree does NOT Support DTD validation.
Which ones do?
I was wondering which of the built in python libraries, support DTD validation.
I've been looking around and haven't been able to get a straight answer. I know ElementTree does NOT Support DTD validation.
Which ones do?
None of the 'builtin' to my knowledge, but libxml2/libxslt comes with python bindings, and will validate easily. The coding model isn't particularly pythonic, but it's sort of the standard others are measured against.
from the examples directory in the libxml2 python bindings:
#!/usr/bin/python -u
import libxml2
import sys
# Memory debug specific
libxml2.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 dtd
del ctxt
I suggest lxml which supports DTD validation. lxml is a Pythonic binding for the libxml2 and libxslt libraries.