解析导入其他本地架构的XML架构时出错

时间:2017-03-21 14:39:28

标签: python xml lxml

我尝试将本地XML架构导入另一个架构,但在解析" parent"时遇到错误。使用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.

我错过了什么?

1 个答案:

答案 0 :(得分:1)

targetNamespace需要导出与namespacexsd: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>

CHR()