Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when parsing XML schema which imports other local schema

Tags:

python

xml

lxml

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?

like image 877
Oskar Persson Avatar asked Sep 10 '25 18:09

Oskar Persson


1 Answers

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>
like image 160
Liza Daly Avatar answered Sep 13 '25 09:09

Liza Daly