I am parsing an xml file generated by an external program. I would then like to add custom annotations to this file, using my own namespace. My input looks as below:
I know this is old question, but it still valid and as of lxml 3.5.0, there is probably better solution to this problem:
cleanup_namespaces()accepts a new argumenttop_nsmapthat moves definitions of the provided prefix-namespace mapping to the top of the tree.
So now the namespace map can be moved up with simple call to this:
nsmap = {'kjw': 'http://this.is.some/custom_namespace'}
etree.cleanup_namespaces(root, top_nsmap=nsmap)