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

I am parsing an inappropriate XML file ( Sphinx xmlpipe2 format ) and would like the LXML parser to ignore the fact that there are unresolved namespace prefixes.

Sphinx XML Example:

<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 am aware of passing a keyword parameter to a parser to try to repair corrupted XML, for example.

 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 that is added to the remote prefix, for example.

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

where AddPrefix() is a class that adds a prefix to each attribute tag. Is there an easier way to do this? In the end, I want to programmatically write the Sphinx xmlpipe2 format.

+7
python xml lxml
source share
1 answer

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

+1
source share

All Articles