在 Python 中使用 lxml 将 XSD 解析为 XML 时遵循 xs:include [英] Following xs:include when parsing XSD as XML with lxml in Python
问题描述
所以,我的问题是我正在尝试做一些不正统的事情.我有一组复杂的 XSD 文件.但是我不想使用这些 XSD 文件来验证 XML 文件;我想将这些 XSD 解析为 XML 并像处理普通 XML 文件一样询问它们.这是可能的,因为 XSD 是有效的 XML.我在 Python3 中使用 lxml.
So, my problem is I'm trying to do something a little un-orthodox. I have a complicated set of XSD files. However I don't want to use these XSD files to verify an XML file; I want to parse these XSDs as XML and interrogate them just as I would a normal XML file. This is possible because XSDs are valid XML. I am using lxml with Python3.
我遇到的问题是语句:
<xs:include schemaLocation="sdm-extension.xsd"/>
如果我指示 lxml 创建一个 XSD 来进行这样的验证:
If I instruct lxml to create an XSD for verifying like this:
schema = etree.XMLSchema(schema_root)
这个依赖关系将被解决(文件与我刚刚加载的文件存在于同一目录中).但是,我将这些视为 XML,因此,正确地,lxml 只是将其视为具有属性的普通元素而不遵循它.
this dependency will be resolved (the file exists in the same directory as the one I've just loaded). HOWEVER, I am treating these as XML so, correctly, lxml just treats this as a normal element with an attribute and does not follow it.
是否有一种简单或正确的方法来扩展 lxml,以便我可能具有相同或相似的行为,例如
Is there an easy or correct way to extend lxml so that I may have the same or similar behaviour as, say
<xi:include href="metadata.xml" parse="xml" xpointer="title"/>
当然,我可以手动创建一个单独的 xml 文件,其中包含 XSD 架构中的所有依赖项.这也许是一个解决方案?
I could, of course, create a separate xml file manually that includes all the dependencies in the XSD schema. That is perhaps a solution?
推荐答案
试试这个:
def validate_xml(schema_file, xml_file):
xsd_doc = etree.parse(schema_file)
xsd = etree.XMLSchema(xsd_doc)
xml = etree.parse(xml_file)
return xsd.validate(xml)
这篇关于在 Python 中使用 lxml 将 XSD 解析为 XML 时遵循 xs:include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!