使用 opengis xsd 验证 xml

分享于2023年02月01日 lxml opengis python xml xmllint 问答
【问题标题】:Use an opengis xsd to validate an xml使用 opengis xsd 验证 xml
【发布时间】:2023-01-29 19:06:54
【问题描述】:

我正在尝试使用从 opengis 下载的 xsd 来验证 xml。

我从 http://schemas.opengis.net/iso/19139/ (版本 20060504 )下载了 xsd 文件。

我想使用 lxml 在 python 中加载我需要的 xsd ( gmd.xsd )。因为有包含我遇到了问题,发现我之前必须做 xinclude() 。现在我用来加载 xsd 的代码是

schema_xml = etree.parse("schema/19139/20060504/gmd/gmd.xsd")
schema_xml.xinclude()
schema = etree.XMLSchema(schema_xml)

但它失败并出现此错误

XMLSchemaParseError                       Traceback (most recent call last)
Cell In[146], line 1
----> 1 schema = etree.XMLSchema(schema_xml)

File src/lxml/xmlschema.pxi:89, in lxml.etree.XMLSchema.__init__()

XMLSchemaParseError: complex type 'EX_TemporalExtent_Type', attribute 'base':
The QName value '{http://www.isotc211.org/2005/gco}AbstractObject_Type' does not
resolve to a(n) simple type definition., line 16

然后我尝试使用 bash 中的 xmllint

xmllint --schema schema/19139/20060504/gmd/gmd.xsd file.xml --noout

这也因一长串错误而失败。前 10 行是

warning: failed to load external entity "http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd"
schema/19139/20060504/gmd/metadataApplication.xsd:8: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd'. Skipping the import.
warning: failed to load external entity "http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd"
schema/19139/20060504/gmd/metadataEntity.xsd:8: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd'. Skipping the import.
warning: failed to load external entity "http://schemas.opengis.net/iso/19139/20060504/gss/gss.xsd"
schema/19139/20060504/gmd/spatialRepresentation.xsd:8: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://schemas.opengis.net/iso/19139/20060504/gss/gss.xsd'. Skipping the import.
warning: failed to load external entity "http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd"
schema/19139/20060504/gmd/spatialRepresentation.xsd:9: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd'. Skipping the import.
warning: failed to load external entity "http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd"
schema/19139/20060504/gmd/citation.xsd:8: element import: Schemas parser warning : Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://schemas.opengis.net/iso/19139/20060504/gco/gco.xsd'. Skipping the import.

有任何想法吗?


【解决方案1】:

我成功地使用了 xmlschema 来验证 XML 反对 xsd 架构 Doc ):

import xmlschema

schema = xmlschema.XMLSchema('xml_validate.xsd')
res = schema.is_valid('xml_validate.xml')
print(res)

如果 xml 有效,则输出应为 True

【讨论】: