How can I fix an XSD import error in lxml?

I run this check with lxml:

parser = etree.XMLParser() try: root = etree.fromstring(xml_content.strip(), parser) except Exception as e: raise XMLFormatException(str(e), XMLFormatException.IN_XML) try: schema = etree.XMLSchema(etree.XML(xsd_content.strip())) except Exception as e: raise XMLFormatException(str(e), XMLFormatException.IN_XSD) if not schema.validate(): raise XMLValidationException("Se produjo un error al validar el XML", schema.error_log) 

Suppose xml_content and xsd_content are created correctly. Part of the contents of xsd is this:

 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" elementFormDefault="qualified"> <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd" /> <!-- more stuff here --> </xsd:schema> 

When I run the script, I get an error message:

Failed to load external object "xmldsig-core-schema.xsd"

When I hit http://www.w3.org/2000/09/xmldsig# in a browser, I get the contents of xsd.

Q : What am I missing here? How to avoid such an error?

Change Notes:

  • This error occurs before validation can be performed (i.e., this error occurs in the XMLSchema constructor).
  • The xsd file is provided from an external source and I am not allowed to edit it.
+6
source share
1 answer

Make sure you have a copy of xmldsig-core-schema.xsd in the same directory as the importing XSD.

If you want to place the imported XSD elsewhere in your file system, you can use absolute paths in URI notation. For example, on Windows:

 <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="file:///c:/path/to/your/xsd/xmldsig-core-schema.xsd" /> 

Or change this:

 <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd" /> 

:

 <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/2000/09/xmldsig" /> 

to access the remote copy that you checked is located at this endpoint.

+2
source

Source: https://habr.com/ru/post/979941/


All Articles