开发者

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

开发者 https://www.devze.com 2023-01-12 11:19 出处:网络
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.

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 pref开发者_JAVA技巧ix to every attribute tag. Is there a simpler way to do this? Eventually i want to programmatically write Sphinx's xmlpipe2 format cleanly.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消