I want to add doctypes to my XML documents that I’m generating with LXML’s etree.
However I cannot figure out how to add a doctype. Hardcoding and concating the string is not an option.
I was expecting something along the lines of how PI’s are added in etree:
pi = etree.PI(...)
doc.addprevious(pi)
But it’s not working for me. How to add a to a xml document with lxml?
You can create your document with a doctype to begin with:
prints:
If you want to add a doctype to some XML that wasn’t created with one, you can first create one with the desired doctype (as above), then copy your doctype-less XML into it:
prints:
Is that what you’re looking for?