tags:

views:

29

answers:

0

I'm parsing a non-compliant xml file (Sphinx's[1] 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] http://www.sphinxsearch.com/