I have some XML file i want to validate and i have to do it with Python. I've tryed to validate it with XSD with lxml. But i get only one error which occurs first but i need all errors and mismatches in XML file. Is there any method how i can manage to get list of all errors with lxml? Or are there any other Python solutions?
The way to solve this problem was:
try: xmlschema.assertValid(xml_to_validate) except etree.DocumentInvalid, xml_errors: pass print "List of errors:\r\n", xml_errors.error_log
May be there are better ways to solve this problem :)
With lxml, you can iterate over error_log and print line number and error message for each error:
def validate_with_lxml(xsd_tree, xml_tree):
schema = lxml.etree.XMLSchema(xsd_tree)
try:
schema.assertValid(xml_tree)
except lxml.etree.DocumentInvalid:
print("Validation error(s):")
for error in schema.error_log:
print(" Line {}: {}".format(error.line, error.message))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With