Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

xml

lxml

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.

like image 699
chris Avatar asked Aug 19 '10 11:08

chris


1 Answers

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

like image 144
Chipaca Avatar answered Sep 24 '22 01:09

Chipaca