xmlschema icon indicating copy to clipboard operation
xmlschema copied to clipboard

Question: How to aggregate several errors?

Open jnussbaum opened this issue 1 year ago • 2 comments

Hi

First, thanks for your nice package! It's the only XML validator I know that supports XSD 1.1 😄

I have a question: Currently, we use XSD 1.0, and lxml.etree.XMLSchema as our validator. That package has the advantage that if there are several validation errors, it has an error_log that you can iterate over. I didn't find an equivalent for this in your package.

Is there a way how to aggregate several errors with your package?

This is my code:

schema = xmlschema.XMLSchema11("schema.xsd")
try:
    schema.validate("data.xml")
except xmlschema.XMLSchemaValidationError as e:
    error_msg = f"Your XML file contains the following error: {e.reason}"
    # here, I'd like to have info about not only the 1st error, but all of them

Thanks in advance for your help 😃

jnussbaum avatar Aug 23 '24 14:08 jnussbaum

Hi,

thank you for appreciations :sweat_smile: ...

To obtain all errors listify the errors returned by iter_errors() API:

schema = xmlschema.XMLSchema11("schema.xsd")
errors = list(schema.iter_errors("data.xml"))

Furthermore, if you need to decode XML data the decode() API has a lax validation mode that returns decoded data and the list of errors:

schema = xmlschema.XMLSchema11("schema.xsd")
data, errors = schema.decode("data.xml", validation='lax')

brunato avatar Aug 24 '24 11:08 brunato

Thank you very much for your reply, and sorry that I didn't see this myself in the documentation.

jnussbaum avatar Aug 26 '24 15:08 jnussbaum