31

I need to validate an XML string (and not a file) against a DTD description file.

How can that be done in python?

Michael Schmidt
  • 9,090
  • 13
  • 56
  • 80
fulmicoton
  • 15,502
  • 9
  • 54
  • 74

2 Answers2

32

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 StringIO

from lxml import etree

dtd = etree.DTD(StringIO("""<!ELEMENT foo EMPTY>"""))
root = etree.XML("<foo/>")
print(dtd.validate(root))
# True

root = etree.XML("<foo>bar</foo>")
print(dtd.validate(root))
# False
print(dtd.error_log.filter_from_errors())
# <string>:1:0:ERROR:VALID:DTD_NOT_EMPTY: Element foo was declared EMPTY this one has content
robsn
  • 734
  • 5
  • 18
Michael Twomey
  • 1,722
  • 14
  • 7
7

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 Nov 07 '08 at 15:42