If you use lxml.etree
, you can write an XML processing instruction (<? ... ?>
) like this:
>>> from lxml import etree
>>> doc = etree.ElementTree(etree.fromstring('<data/>'))
>>> doc.getroot().addprevious(etree.ProcessingInstruction('xml', 'version=1.0'))
>>> print etree.tostring(doc)
<?xml version=1.0?><data/>
You can set doctype attribute when writing your XML by setting the doctype
parameter of, e.g., etree.tostring
:
>>> print etree.tostring(doc, doctype='<!doctype email-template PUBLIC "-//yourcompany, Inc.//DTD email-template//EN" "email-template.dtd">')
<!doctype email-template PUBLIC "-//yourcompany, Inc.//DTD email-template//EN" "email-template.dtd">
<?xml version=1.0?><data/>