Is there a switch to ignore undefined namespace prefixes in LXML?

☆樱花仙子☆ 提交于 2019-12-03 12:45:32

问题


I'm parsing a non-compliant XML file (Sphinx's xmlpipe2 format) and would like LXML parser to ignore the fact that there are unresolved namespace prefixes.

An example of the Sphinx XML:

<sphinx:schema>
    <sphinx:field name="subject"/>
    <sphinx:field name="content"/>
    <sphinx:attr name="published" type="timestamp"/>
    <sphinx:attr name="author_id" type="int" bits="16" default="1"/>
</sphinx:schema>

I'm aware of passing a parser keyword option to try and recover broken XML, e.g.

parser = etree.XMLParser(recover=True)
tree = etree.parse('sphinxTest.xml', parser)

but the above does not ignore the prefix, it removes it.

I could create a target which adds in the removed prefix e.g.

parser = etree.XMLParser(target = AddPrefix())

where AddPrefix() is a class which adds in the prefix to every attribute tag. Is there a simpler way to do this? Eventually i want to programmatically write Sphinx's xmlpipe2 format cleanly.


回答1:


Add xmlns:sphinx="bogus" to the root element.



来源:https://stackoverflow.com/questions/3521140/is-there-a-switch-to-ignore-undefined-namespace-prefixes-in-lxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!