lxml: add namespace to input file

后端 未结 6 1551
不思量自难忘°
不思量自难忘° 2020-12-03 21:16

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:

6条回答
  •  臣服心动
    2020-12-03 21:36

    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 argument top_nsmap that 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)
    

提交回复
热议问题