Install DTD using minidisk in python

I am trying to include a link to a DTD in my XML document using minidom.

I create a document as:

doc = Document() foo = doc.createElement('foo') doc.appendChild(foo) doc.toxml() 

This gives me:

 <?xml version="1.0" ?> <foo/> 

I need to get something like:

 <?xml version="1.0" ?> <!DOCTYPE something SYSTEM "http://www.path.to.my.dtd.com/my.dtd"> <foo/> 
+6
python xml minidom dtd
source share
2 answers

The documentation is out of date. Use the source, Luke. I do it something like this.

 from xml.dom.minidom import DOMImplementation imp = DOMImplementation() doctype = imp.createDocumentType( qualifiedName='foo', publicId='', systemId='http://www.path.to.my.dtd.com/my.dtd', ) doc = imp.createDocument(None, 'foo', doctype) doc.toxml() 

The following is printed.

 <?xml version="1.0" ?><!DOCTYPE foo SYSTEM \'http://www.path.to.my.dtd.com/my.dtd\'><foo/> 

Note that the root element is created automatically using createDocument (). In addition, your β€œsomething” has been changed to β€œfoo”: the DTD must contain the name of the root element itself.

+9
source share

According to Python docs, the implementation of the DocumentType interface is not implemented in the mini-minim.

+1
source share

All Articles