Python lxml(etree.XMLSchema)中的潜在错误:不';t验证xml文档中的IDREF关系
我使用lxml-XMLSchema对象根据指定xs:ID和xs:IDREF约束的xsd验证xml文件。然而,即使IDREF没有相应的ID,lxml显然也会将xml文档验证为true 关于如何解决这个问题有什么想法吗 编辑: XSD:Python lxml(etree.XMLSchema)中的潜在错误:不';t验证xml文档中的IDREF关系,python,xsd,lxml,idref,Python,Xsd,Lxml,Idref,我使用lxml-XMLSchema对象根据指定xs:ID和xs:IDREF约束的xsd验证xml文件。然而,即使IDREF没有相应的ID,lxml显然也会将xml文档验证为true 关于如何解决这个问题有什么想法吗 编辑: XSD: 对于给定的XSD/XML,此函数返回True,即使元素Bad的属性moduleId违反了其IDREF约束请显示您正在使用的代码。你需要给我们一个重现问题的方法。谢谢,很抱歉。我已经包括了XSD、XML和验证代码。 <xs:schema xmlns:xs="h
对于给定的XSD/XML,此函数返回True,即使元素Bad的属性moduleId违反了其IDREF约束请显示您正在使用的代码。你需要给我们一个重现问题的方法。谢谢,很抱歉。我已经包括了XSD、XML和验证代码。
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.chaos.com"
xmlns="http://www.chaos.com"
elementFormDefault="qualified">
<xs:complexType name="ModuleType">
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="path" type="xs:string" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:complexType name="ModulesType" mixed="true">
<xs:sequence>
<xs:element name="Module" type="ModuleType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:complexType name="FrameworkType" mixed="true">
<xs:all>
<xs:element name="Good">
<xs:complexType>
<xs:attribute name="moduleId" type="xs:IDREF" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
</xs:element>
<xs:element name="Bad">
<xs:complexType>
<xs:attribute name="moduleId" type="xs:IDREF" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
</xs:element>
</xs:all>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:complexType name="TestbenchType" mixed="true">
<xs:sequence>
<xs:element name="Modules" type="ModulesType"/>
<xs:element name="Framework" type="FrameworkType"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Testbench" type="TestbenchType"/>
</xs:schema>
<Testbench xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.chaos.com">
<Modules>
<Module id="Module1" path="Testbench_module1.py"/>
<Module id="Module2" path="Testbench_module2.py"/>
</Modules>
<Framework>
<Good moduleId="Module1"/>
<Bad moduleId="ModuleX"/>
</Framework>
</Testbench>
from lxml import etree
from cStringIO import StringIO
try:
#Get schema
buf_schema = StringIO()
with open(schema,'r') as fh:
for line in fh: buf_schema.write(line)
buf_schema.seek(0)
Schema = etree.XMLSchema(etree.parse(buf_schema))
buf_schema.close()
# Validate
buf_cfg = StringIO()
with open(cfg,'r') as fh:
for line in fh: buf_cfg.write(line)
buf_cfg.seek(0)
Schema.assertValid(etree.parse(buf_cfg))
buf_cfg.close()
except etree.XMLSyntaxError, err:
return False, str(err)
except Exception, err:
return False, str(err)
return True,None