以下是引用有效XML模式(1.xsd)和任意XML(1.XML)的python代码:
1.xsd:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="product"/>
</xs:schema>
1.xml:
^{pr2}$主.py:
from lxml.etree import XMLParser, XMLSchema, parse, XMLSyntaxError
parser = XMLParser(schema=XMLSchema(file='1.xsd'))
try:
xml = parse(open('1.xml', mode='rb'), parser)
except XMLSyntaxError as e:
for error in parser.error_log:
print error.message, error.line, error.column
输出:
Element 'my-product': No matching global declaration available for the validation root. 0 0
错误.line和错误.列总是0。 我怎样才能得到一个错误的位置?在
升级版
此代码给出了正确的位置,但缺少双重解析:
from lxml.etree import XMLParser, XMLSchema, parse, XMLSyntaxError, XML
schema=XMLSchema(file='1.xsd')
parser = XMLParser(schema=schema)
xml_file = open('1.xml', mode='rb')
xml = XML(xml_file.read())
if not schema.validate(xml):
for error in schema.error_log:
print error.message, error.line, error.column
else:
xml = parse(xml_file, parser)
Element 'my-product': No matching global declaration available for the validation root. 2 0
目前没有回答
相关问题 更多 >
编程相关推荐