Python Elementtree:忽略未定义的前缀

发布于 2024-12-11 05:09:47 字数 181 浏览 4 评论 0原文

我有很多从各种来源收集的 XML“片段”。由于这些不是完全形成的 XML 文档,因此相当多的文档没有声明其标签前缀,例如 等标签。段落文本

使用 Python 中的 ElementTree 来解析这些文本,经常会遇到无界前缀错误。是否有一个开关可以让解析器一起忽略前缀?

I have a lot of XML 'fragments' that are collected from a variety of sources. Since these are not fully formed XML documents, quite a few do not have their tag prefixes declared, e.g. tags like <exch:p> paragraph text </exch:p>

Using ElementTree in Python to parse these, very often I get the unbounded prefix error. Is there a switch to make the parser ignore the prefixes all together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2024-12-18 05:09:47

我认为 lxml 和 elementtree 不能同时使用。 XMLParser 上的选项是有限的。错误消息来自 expat 解析器库。处理它的方法是在解析之前声明一个名称空间。

在 XPath 中,有一种方法可以仅获取本地名称

//*[local-name()='p']

,但正如文档中提到的,XPath 并未在 elementtree 中完全实现。

I do not think you can with both lxml and elementtree. The options on XMLParser are limited. The error message is coming from the expat parser library. The way to deal with it would be to declare a namespace before the parsing.

In XPath, there is a way to only get the local-name

//*[local-name()='p']

but XPath is not fully implemented in elementtree as mentionned in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文