XML Declaration standalone="yes" lxml
You can pass standalone
keyword argument to tostring()
:
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=True)
Specify standalone
using tree.docinfo.standalone.
Try following:
from lxml import etreetree = etree.fromstring(templateXml).getroottree() # NOTE: .getroottree()xmlFileOut = '/Users/User1/Desktop/Python/Done.xml' with open(xmlFileOut, "w") as f: f.write(etree.tostring(tree, pretty_print=True, xml_declaration=True, encoding=tree.docinfo.encoding, standalone=tree.docinfo.standalone))
If you want to show the standalone='no'
argument in your XML header, you have to set it to False
instead of 'no'. Just like this:
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=False)
If not, standalone will be set to 'yes' by default.