tags:

views:

443

answers:

3

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?

A: 

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.

reedstrm
+4  A: 

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
Note that the libxml2 bindings are not part of the Python standard library, i.e. not built in.
ChuckB
+3  A: 

I suggest lxml which supports DTD validation. lxml is a Pythonic binding for the libxml2 and libxslt libraries.

bhadra