Oskar Persson
Oskar Persson

Reputation: 6743

Error when parsing XML schema which imports other local schema

I'm trying to import a local XML schema into another but I'm getting an error when parsing the "parent" using lxml:

# main.py

from lxml import etree

if __name__ == '__main__':
    s = etree.fromstring('''
        <xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified">
             <xsd:import
                  namespace="http://www.w3schools.com"
                  schemaLocation="file:///Users/Oskar/test.xsd"/>
        </xsd:schema>
    ''')

    etree.XMLSchema(s)

# test.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="foo" type="xsd:integer"/>
</xsd:schema>

$ python main.py
Traceback (most recent call last):
  File "main.py", line 14, in <module>
    etree.XMLSchema(s)
  File "src/lxml/xmlschema.pxi", line 87, in lxml.etree.XMLSchema.__init__ (src/lxml/lxml.etree.c:191759)
lxml.etree.XMLSchemaParseError: Internal error: xmlSchemaBucketCreate, failed to add the schema bucket to the hash.

What am I missing?

Upvotes: 3

Views: 2138

Answers (1)

Liza Daly
Liza Daly

Reputation: 2963

test.xsd needs to export a targetNamespace which matches the value of namespace in xsd:import:

<xsd:schema
      targetNamespace="http://www.w3schools.com"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="foo" type="xsd:integer"/>
</xsd:schema>

Upvotes: 2

Related Questions