I have tried to use the answer in this question, but can\'t make it work: How to create "virtual root" with Python's ElementTree?
Here\'s my code:
I couldn't find a solution to this problem either using vanilla ElementTree, and the solution proposed by demalexx created non-valid XML that was rejected by my application (DITA). What I propose is a workaround involving other modules and it works perfectly for me.
import re
# found no way for cleanly specify a stanza in ElementTree so
# so we substitute the current stanza with a full \n' \
'\n'
target_xml = re.sub(u"\<\?xml .+?>", new_header, source_xml)
with open(filename, 'w') as catalog_file:
catalog_file.write(target_xml.encode('utf8'))