Python lxml在写入文件时验证xml,并进行读回,但不验证生成文件的DOM树

Python lxml在写入文件时验证xml,并进行读回,但不验证生成文件的DOM树,python,xml,validation,lxml,Python,Xml,Validation,Lxml,我正在使用lxml生成xml并对其进行验证。我发现,当我尝试使用XSD验证器验证DOM树时,验证器会抱怨它无法识别根元素;相反,当该树转换为文本、写入文件、读回并解析为DOM树时,新DOM树的根元素会被识别。希望下面能说明我的意思 我的问题是:为什么会发生这种情况?除了呈现为字符串和重新分析外,是否有其他方法可以让原始DOM树进行验证 >>> from lxml import etree >>> import io >>> validator

我正在使用lxml生成xml并对其进行验证。我发现,当我尝试使用XSD验证器验证DOM树时,验证器会抱怨它无法识别根元素;相反,当该树转换为文本、写入文件、读回并解析为DOM树时,新DOM树的根元素会被识别。希望下面能说明我的意思

我的问题是:为什么会发生这种情况?除了呈现为字符串和重新分析外,是否有其他方法可以让原始DOM树进行验证

>>> from lxml import etree
>>> import io
>>> validator = etree.XMLSchema(etree.parse(io.open('mainapp/xsd/forms/CompanyIncorporation-v2-6.xsd')))
>>> c = CompanyUK.objects.all()[5]
>>> cotree = xml.Company.generate_company_incorporation_tree(c)
>>> cotree
<Element CompanyIncorporation at 0x38f2750>
>>> validator.assertValid(cotree)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 3006, in lxml.etree._Validator.assertValid (src/lxml/lxml.etree.c:125415)
DocumentInvalid: Element 'CompanyIncorporation': No matching global declaration available for the validation root.
>>> cott = etree.ElementTree(cotree)
>>> validator.assertValid(cott)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 3006, in lxml.etree._Validator.assertValid (src/lxml/lxml.etree.c:125415)
DocumentInvalid: Element 'CompanyIncorporation': No matching global declaration available for the validation root.
>>> xmlfile = open("xmlfile", "w")
>>> xmlfile.write(etree.tostring(cott))
>>> xmlfile.flush()
>>> xmlfile.close()
>>> xmlfile = open("xmlfile", "r")
>>> ptree = etree.parse(xmlfile)
>>> validator.assertValid(ptree)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 3006, in lxml.etree._Validator.assertValid (src/lxml/lxml.etree.c:125415)
DocumentInvalid: Element '{http://xmlgw.companieshouse.gov.uk}Country': [facet 'enumeration'] The value 'EW' is not an element of the set {'GB-ENG', 'GB-WLS', 'GB-SCT', 'GB-NIR', 'GBR', 'UNDEF'}., line 1
>>> etree.tostring(ptree) == etree.tostring(cott)
True
>>> validator.assertValid(etree.parse(io.StringIO(etree.tounicode(ptree))))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 3006, in lxml.etree._Validator.assertValid (src/lxml/lxml.etree.c:125415)
DocumentInvalid: Element '{http://xmlgw.companieshouse.gov.uk}Country': [facet 'enumeration'] The value 'EW' is not an element of the set {'GB-ENG', 'GB-WLS', '
GB-SCT', 'GB-NIR', 'GBR', 'UNDEF'}., line 1
>>> validator.assertValid(etree.parse(io.StringIO(etree.tounicode(cot))))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'cot' is not defined
>>> validator.assertValid(etree.parse(io.StringIO(etree.tounicode(cott))))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 3006, in lxml.etree._Validator.assertValid (src/lxml/lxml.etree.c:125415)
DocumentInvalid: Element '{http://xmlgw.companieshouse.gov.uk}Country': [facet 'enumeration'] The value 'EW' is not an element of the set {'GB-ENG', 'GB-WLS', '
GB-SCT', 'GB-NIR', 'GBR', 'UNDEF'}., line 1
>>> validator.assertValid(etree.parse(io.BytesIO(etree.tostring(cott))))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "lxml.etree.pyx", line 3006, in lxml.etree._Validator.assertValid (src/lxml/lxml.etree.c:125415)
DocumentInvalid: Element '{http://xmlgw.companieshouse.gov.uk}Country': [facet 'enumeration'] The value 'EW' is not an element of the set {'GB-ENG', 'GB-WLS', '
GB-SCT', 'GB-NIR', 'GBR', 'UNDEF'}., line 1
>>>
来自lxml导入etree的
>>
>>>输入io
>>>validator=etree.XMLSchema(etree.parse(io.open('mainapp/xsd/forms/CompanyIncorporation-v2-6.xsd'))
>>>c=CompanyUK.objects.all()[5]
>>>cotree=xml.Company.generate\u Company\u incorporation\u树(c)
>>>子树
>>>validator.assertValid(子树)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lxml.etree.\u Validator.assertValid(src/lxml/lxml.etree.c:125415)中的文件“lxml.etree.pyx”,第3006行
DocumentInvalid:元素“companyCorporation”:没有可用于验证根的匹配全局声明。
>>>cott=树元素树(子树)
>>>validator.assertValid(cott)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lxml.etree.\u Validator.assertValid(src/lxml/lxml.etree.c:125415)中的文件“lxml.etree.pyx”,第3006行
DocumentInvalid:元素“companyCorporation”:没有可用于验证根的匹配全局声明。
>>>xmlfile=open(“xmlfile”,“w”)
>>>write(etree.tostring(cott))
>>>xmlfile.flush()
>>>xmlfile.close()
>>>xmlfile=open(“xmlfile”、“r”)
>>>ptree=etree.parse(xmlfile)
>>>validator.assertValid(ptree)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lxml.etree.\u Validator.assertValid(src/lxml/lxml.etree.c:125415)中的文件“lxml.etree.pyx”,第3006行
文档无效:元素的{http://xmlgw.companieshouse.gov.uk}Country':[facet'enumeration']值'EW'不是集合{'GB-ENG','GB-WLS','GB-SCT','GB-NIR','GBR','UNDE'}的元素,第1行
>>>etree.tostring(ptree)==etree.tostring(cott)
真的
>>>assertValid(etree.parse(io.StringIO(etree.tounicode(ptree)))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lxml.etree.\u Validator.assertValid(src/lxml/lxml.etree.c:125415)中的文件“lxml.etree.pyx”,第3006行
文档无效:元素的{http://xmlgw.companieshouse.gov.uk}Country':[facet'enumeration']值'EW'不是集合{'GB-ENG','GB-WLS','
GB-SCT’、‘GB-NIR’、‘GBR’、‘UNDEF’},第1行
>>>assertValid(etree.parse(io.StringIO(etree.tounicode(cot)))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“cot”
>>>assertValid(etree.parse(io.StringIO(etree.tounicode(cott)))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lxml.etree.\u Validator.assertValid(src/lxml/lxml.etree.c:125415)中的文件“lxml.etree.pyx”,第3006行
文档无效:元素的{http://xmlgw.companieshouse.gov.uk}Country':[facet'enumeration']值'EW'不是集合{'GB-ENG','GB-WLS','
GB-SCT’、‘GB-NIR’、‘GBR’、‘UNDEF’},第1行
>>>assertValid(etree.parse(io.BytesIO(etree.tostring(cott)))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lxml.etree.\u Validator.assertValid(src/lxml/lxml.etree.c:125415)中的文件“lxml.etree.pyx”,第3006行
文档无效:元素的{http://xmlgw.companieshouse.gov.uk}Country':[facet'enumeration']值'EW'不是集合{'GB-ENG','GB-WLS','
GB-SCT’、‘GB-NIR’、‘GBR’、‘UNDEF’},第1行
>>>

正如您在上面看到的,验证会随着从磁盘解析的版本而进一步,即使它们呈现为相同的字符串(同样,验证在往返字符串之后会超出根元素,并重新解析为DOM树)。那么,是什么导致了这种情况?

CompanyUK.objects.all()[5]的返回值的类型是什么?@DerekLitz:CompanyUK-这是一个django模型。