Home > Net >  Use an opengis xsd to validate an xml
Use an opengis xsd to validate an xml

Time:02-01

I am trying to use an xsd downloaded from opengis to validate an xml.

I downloaded the xsd files from http://schemas.opengis.net/iso/19139/ (version 20060504).

I wanted to load the xsd I need (gmd.xsd) in python using lxml. Since there are includes I had problems and found out I had to do xinclude() before. Now the code I'm using to load the xsd is

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

But it fails with this error

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

I then tried using xmllint from bash

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

Which also fails with a long series of errors. The first 10 lines are

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.

Any ideas?

CodePudding user response:

I used xmlschema succesfully for validation of xml against an xsd schema (Doc):

import xmlschema

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

Output should be True in case of valid xml.

  • Related