如何使用 python 的 lxml 在 xml 文档中打印实体编号而不是实体名称?
我正在使用 lxml 和 python 生成 xml 文档(仅使用 etree.tostring(root) ),但目前生成的 xml 显示 html 实体与命名实体 ( < ; ) 而不是它们的数值 ( &# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终,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.