Reputation: 742
I have an XML document which I am reading and appending to using ElementTree.
This has multiple namespaces declared. From what I can tell, ElementTree will only allow a single global namespace to be declared:
ET.register_namespace(prefix, uri)
I would like to write multiple namespaces out in the resulting file. If I try to manually append them using
root.set(prefix, uri)
Then they are put in the wrong order, and I get cElementTree.ParseError: unbound prefix: line 2, column 0
Edit: it turns out that I had a typo in the option I was setting. I corrected this, and it re-parses fine. So it looks like this is a viable solution.
Thanks!
Upvotes: 2
Views: 1917
Reputation: 11203
If you are not restricted to just using the standard library, I suggest you install and use the lxml package which provides the ElementTree
API, and it has a number of advantages, including easily allowing you to have multiple namespaces in the same document.
Upvotes: 1