如何跳过lxml中的URI验证? [英] How do I skip validating the URI in lxml?
问题描述
我正在使用lxml解析一些xml文件.我没有创建它们,我只是在解析它们.一些文件包含用于命名空间的无效uri.例如:
I am using lxml to parse some xml files. I don't create them, I'm just parsing them. Some of the files contain invalid uri's for the namespaces. For instance:
'D:\Path\To\some\local\file.xsl'
当我尝试处理它时出现错误:
I get an error when I try to process it:
lxml.etree.XMLSyntaxError: xmlns:xsi: 'D:\Path\To\some\local\file.xsl' is not a valid URI
是否有一种简便的方法可以将某些无效的uri替换为某些内容(例如" http://www. googlefsdfsd.com/')?我想写一个正则表达式,但是希望有一种更简单的方法.
Is there an easy way to replace any invalid uri's with something (anything, such as 'http://www.googlefsdfsd.com/')? I thought of writing a regex but was hoping for an easier way.
推荐答案
解析器不喜欢的是名称空间uri中的反斜杠.
What the parser doesn't like are the backslashes in the namespace uri.
要在无效的uri情况下解析xml,可以实例化 lxml.etree recover
参数设置为True
的.XMLParser ,然后使用它来解析文件:
To parse the xml despite the invalid uris, you can instantiate an lxml.etree.XMLParser with the recover
argument set to True
and then use that to parse the file:
from lxml import etree
recovering_parser = etree.XMLParser(recover=True)
xml = etree.parse("xmlfile.xml", parser=recovering_parser)
...
这篇关于如何跳过lxml中的URI验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!