如何解决Python lxml 没有可用于验证根的匹配全局声明
我正在使用 lxml 解析器根据 XML 架构验证 xml 文档。xml 文档是:
<?xml version="1.0" encoding="UTF-8"?>
<KnittingModel modelname="mymodel"
xmlns = "http://www.ludd21.com/KnittingModel">
<Piece piecename="manche">
<kPartList>kp1</kPartList>
<kPartList>kp2</kPartList>
<kPartList>kp3</kPartList>
</Piece>
<Piece piecename="devant">
<kPartList>kp4</kPartList>
<kPartList>kp5</kPartList>
<kPartList>kp6</kPartList>
<kPartList>kp7</kPartList>
</Piece>
</KnittingModel>
XMLSchema 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.ludd21.com/KnittingModel"
xmlns = "http://www.ludd21.com/KnittingModel"
elementFormDefault="qualified">
<!--targetNameqpace specifies the namespace part of the name of all top-level elements -->
<!--attributes and types declared in the schema document -->
<!--xlmns attribute specifies how unprefixed names will be interpreted-->
<!--<xs:import schemaLocation = "kPart.xsd"-->
<!--namespace = "http://ludd21.com/KPartTypes" />-->
<xsd:annotation>
<xsd:documentation xml:lang="fr">
Schéma XML pour un modèle de tricot
</xsd:documentation>
</xsd:annotation>
<xsd:element name="knittingModel" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name = 'modelname' type = 'xsd:string'/>
<xsd:element name="piece" minOccurs="1" maxOccurs="unbounded" type="Piece"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="KPart" >
<xsd:restriction base = 'xsd:string'>
<xsd:maxLength value = "5" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="KPartList">
<xsd:sequence>
<xsd:element name="kPart" minOccurs="1" maxOccurs="unbounded" type = "KPart"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Piece" >
<xsd:sequence>
<xsd:element name="piecename" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="kPartList" type = "KPartList"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我收到以下错误:lxml.etree.XMLSyntaxError: Element '{http://www.ludd21.com/KnittingModel}KnittingModel': 没有匹配的全局声明可用于验证根。 (,第 0 行)
调用解析器的python指令是:
parser = ET.XMLParser(schema=xmlschema)
root = ET.fromstring(bytes(xmlknittingmodel,'utf-8'),parser)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。