【发布时间】:2015-11-23 21:27:16
【问题描述】:
我正在尝试使用 lxml 针对 B2MML 模式(多个 XSD 文档)验证一个简单的 XML 文档。 lxml 返回“没有可用于验证根的匹配全局声明。”此类问题之前已发布,但似乎没有一个解决方案有效。
需要验证的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<MaterialLot xmlns="http://www.w3.org/2001/XMLSchema">
<ID>000010002377</ID>
</MaterialLot>
用于验证文档的代码:
def validate_xml(xmlschemafile, xmlfilename):
with open(xmlschemafile, 'r') as f:
schema_root = etree.XML(f.read())
schema = etree.XMLSchema(schema_root)
xmlparser = etree.XMLParser(schema=schema)
try:
with open(xmlfilename, 'r') as f:
etree.fromstring(f.read(), xmlparser)
return True
if __name__ == '__main__':
validate_xml(xmlschema, filename)
B2MML 架构文件位于此处: https://github.com/werdnav/b2mml-validation
XSD 名称空间是“http://www.w3.org/2001/XMLSchema”(从 XSD 文档中读取),它与 XML 示例完全匹配。但是,验证器代码(上面)返回:
Element '{http://www.w3.org/2001/XMLSchema}MaterialLot': No matching global declaration available for the validation root.
这很奇怪,因为 MaterialLot 是全局声明的。
【问题讨论】:
标签: python xml validation xsd