Python: namespaces in xml ElementTree (or lxml)

前端 未结 2 818
无人及你
无人及你 2021-01-17 23:05

I want to retrieve a legacy xml file, manipulate and save it.

Here is my code:

from xml.etree import cElementTree as ET
NS = \"{http://www.somedomain         


        
2条回答
  •  误落风尘
    2021-01-17 23:30

    To answer your questions in order:

    • you can't just ignore the namespace, not in the path syntax that .findall() uses , but not in "real" xpath (supported by lxml) either: there you'd still be forced to use a prefix, and still need to provide some prefix-to-uri mapping.

    • use xml_declaration=True as well as encoding='utf-8' with the .write() call (available in lxml, but in stdlib xml.etree only since python 2.7 I believe)

    • I believe lxml will do behave like you want

提交回复
热议问题