是否可以忽略LXML中未定义的名称空间前缀? [英] Is there a switch to ignore undefined namespace prefixes in LXML?

查看:62
本文介绍了是否可以忽略LXML中未定义的名称空间前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析不兼容的XML文件( Sphinx的 xmlpipe2格式),并且希望使用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.

Sphinx XML的示例:

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>

我知道传递解析器关键字选项来尝试恢复损坏的XML,例如

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())

其中,AddPrefix()是一个类,它在每个属性标签中添加前缀. 有没有更简单的方法可以做到这一点? 最终,我想以编程方式清晰地编写Sphinx的xmlpipe2格式.

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.

推荐答案

在根元素中添加xmlns:sphinx="bogus".

这篇关于是否可以忽略LXML中未定义的名称空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆