如何使用 python 的 lxml 在 xml 文档中打印实体编号而不是实体名称?

发布于 2024-07-16 04:04:48 字数 169 浏览 3 评论 0原文

我正在使用 lxml 和 python 生成 xml 文档(仅使用 etree.tostring(root) ),但目前生成的 xml 显示 html 实体与命名实体 ( &lt ; ) 而不是它们的数值 ( &# 60)。 我究竟该如何更改此设置,以便结果使用数值而不是名称?

谢谢

I'm using lxml and python to generate xml documents (just using etree.tostring(root) ) but at the moment the resulting xml displays html entities as with named entities ( < ; ) rather than their numeric values ( < ; ). How exactly do I go about changing this so that the result uses the numeric values instead of the names?

Thanks

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

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

发布评论

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

评论(1

再见回来 2024-07-23 04:04:48

最终,Python 代码看起来会调用 xmlNodeDumpOutput

不幸的是,看起来没有任何方法可以配置它来控制这些实体的表示方式。 查看 xmlEncodeEntitiesReentrant 中的Entity.c,<< > 和& 字符被硬编码为始终使用适当的 XML 实体,因此似乎没有办法强制它使用数值。

如果您需要这个,您可能需要对字符串执行另一次传递,并手动执行“outputString.replace("<","<")"那些角色。

Ultimately, it looks like the python code will call xmlNodeDumpOutput in the libxml2 library.

Unfortunately, it doesn't look like there is any way to configure this to control how such entities are represented. Looking at entities.c in xmlEncodeEntitiesReentrant, the < > and & characters are hardcoded to always use the appropriate XML entity, so there seems no way to force it to use numeric values.

If you need this, you'll probably have to perform another pass on the string, and manually perform "outputString.replace("<","<")" for those characters.

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