I’m trying to validate an XML document in Python using lxml.
DTD validation will treat the presence of xmlns namespaces as errors.
This example script
from lxml import etree
from StringIO import StringIO
dtd = etree.DTD(StringIO("<!ELEMENT a EMPTY>"))
root = etree.XML("<a></a>")
print(dtd.validate(root))
root = etree.XML('<a xmlns:sru="http://www.loc.gov/zing/srw/"></a>')
print(dtd.validate(root))
print dtd.error_log
yields
True
False
<string>:1:0:ERROR:VALID:DTD_UNKNOWN_ATTRIBUTE: No declaration for attribute xmlns:sru of element a
How can I have lxml validate my document against a DTD without complaining about xml namespace?
You have to define the xmlns:sru attribute for the a element in your DTD. It currently doesn’t have one.
This defines an optional CDATA attribute for xmlns:sru on element a.