With lxml, how can I prepend processing instructions before the root element or append PIs after de root element with lxml.
Currently, the following example doesn't work:
from lxml import etree
root = etree.XML("<ROOT/>")
root.addprevious(etree.ProcessingInstruction("foo"))
print(etree.tounicode(root))
I get:
<ROOT/>
Instead of:
<?foo?><ROOT/>
You need to use
ElementTree, not justElementintounicode():Output is almost what you wanted:
Extra space character after
fooshowed up becauselxmlrendersPIaspi.target + " " + pi.text.