将 XML 目录与 Python 的 lxml 结合使用?

发布于 2024-07-05 03:47:03 字数 80 浏览 6 评论 0原文

当我使用 lxml 解析 XML 文档时,有没有办法使用外部目录文件根据其 DTD 验证该文档? 我需要能够使用文档 DTD 中定义的固定属性。

Is there a way, when I parse an XML document using lxml, to validate that document against its DTD using an external catalog file? I need to be able to work the fixed attributes defined in a document’s DTD.

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

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

发布评论

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

评论(3

顾挽 2024-07-12 03:47:04

你能给个例子吗? 根据 lxml 验证文档,lxml 可以处理 DTD 验证(在 XML 文档中或外部指定)在代码中)和系统目录,涵盖了我能想到的大多数情况。

f = StringIO("<!ELEMENT b EMPTY>")
dtd = etree.DTD(f)
dtd = etree.DTD(external_id = "-//OASIS//DTD DocBook XML V4.2//EN")

Can you give an example? According to the lxml validation docs, lxml can handle DTD validation (specified in the XML doc or externally in code) and system catalogs, which covers most cases I can think of.

f = StringIO("<!ELEMENT b EMPTY>")
dtd = etree.DTD(f)
dtd = etree.DTD(external_id = "-//OASIS//DTD DocBook XML V4.2//EN")
穿透光 2024-07-12 03:47:04

看来lxml没有公开这个libxml2功能,grep源只会出现一些用于错误处理的#define:

C:\Dev>grep -ir --include=*.px[id] catalog lxml-2.1.1/src | sed -r "s/\s+/ /g"
lxml-2.1.1/src/lxml/dtd.pxi: catalog.
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_FROM_CATALOG = 20 # The Catalog module
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_WAR_CATALOG_PI = 93 # 93
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_MISSING_ATTR = 1650
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_ENTRY_BROKEN = 1651 # 1651
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_PREFER_VALUE = 1652 # 1652
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_NOT_CATALOG = 1653 # 1653
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_RECURSION = 1654 # 1654
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG=20
lxml-2.1.1/src/lxml/xmlerror.pxi:WAR_CATALOG_PI=93
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_MISSING_ATTR=1650
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_ENTRY_BROKEN=1651
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_PREFER_VALUE=1652
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_NOT_CATALOG=1653
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_RECURSION=1654

来自 libxml2 页面中的目录实现 通过安装在 /etc/xml/catalog 中进行的“透明”处理似乎仍然可以在 lxml 中工作,但如果您需要更多,您可以随时放弃 lxml 并使用默认值python 绑定,它公开了目录函数。

It seems that lxml does not expose this libxml2 feature, grepping the source only turns up some #defines for the error handling:

C:\Dev>grep -ir --include=*.px[id] catalog lxml-2.1.1/src | sed -r "s/\s+/ /g"
lxml-2.1.1/src/lxml/dtd.pxi: catalog.
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_FROM_CATALOG = 20 # The Catalog module
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_WAR_CATALOG_PI = 93 # 93
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_MISSING_ATTR = 1650
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_ENTRY_BROKEN = 1651 # 1651
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_PREFER_VALUE = 1652 # 1652
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_NOT_CATALOG = 1653 # 1653
lxml-2.1.1/src/lxml/xmlerror.pxd: XML_CATALOG_RECURSION = 1654 # 1654
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG=20
lxml-2.1.1/src/lxml/xmlerror.pxi:WAR_CATALOG_PI=93
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_MISSING_ATTR=1650
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_ENTRY_BROKEN=1651
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_PREFER_VALUE=1652
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_NOT_CATALOG=1653
lxml-2.1.1/src/lxml/xmlerror.pxi:CATALOG_RECURSION=1654

From the catalog implementation in libxml2 page it seems possible that the 'transparent' handling through installation in /etc/xml/catalog may still work in lxml, but if you need more than that you can always abandon lxml and use the default python bindings, which do expose the catalog functions.

|煩躁 2024-07-12 03:47:03

您可以将目录添加到 XML_CATALOG_FILES 环境变量:

os.environ['XML_CATALOG_FILES'] = 'file:///to/my/catalog.xml'

请参阅

You can add the catalog to the XML_CATALOG_FILES environment variable:

os.environ['XML_CATALOG_FILES'] = 'file:///to/my/catalog.xml'

See this thread. Note that entries in XML_CATALOG_FILES are space-separated URLs. You can use Python's pathname2url and urljoin (with file:) to generate the URL from a pathname.

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