Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix an XSD import error in lxml?

Tags:

python

lxml

xsd

I run this validation using 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)

Assume xml_content and xsd_content are correctly instantiated. Part of the xsd content 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:

failed to load external entity "xmldsig-core-schema.xsd"

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

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

Edit Notes:

  1. This error occurs before the validation can be made (i.e. this error occurs in the XMLSchema constructor).
  2. The xsd file is provided from an external source and I'm not allowed to edit it.
like image 621
Luis Masuelli Avatar asked Dec 19 '14 17:12

Luis Masuelli


People also ask

What is XSD import?

Use xsd:include brings all declarations and definitions of an external schema document into the current schema. Use xsd:import to bring in an XSD from a different namespace and used to build a new schema by extending existing schema documents..

How do I import XSD files into Visual Studio?

You can easily just add an existing XSD on disk to your Visual Studio project by doing a "Add Existing Item" and then picking that file. There's no separate "import / export" functionality, really.

How do I check if an XML file is valid in Python?

You can easily validate an XML file or tree against an XML Schema (XSD) with the xmlschema Python package. It's pure Python, available on PyPi and doesn't have many dependencies. The method raises an exception if the file doesn't validate against the XSD.


1 Answers

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

If you wish to located the imported XSD elsewhere in your filesystem, 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" />

to this:

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

to access the remote copy, which you've verified is at that endpoint.

like image 158
kjhughes Avatar answered Sep 20 '22 08:09

kjhughes