sissaschool / xmlschema

XML Schema validator and data conversion library for Python
MIT License
406 stars 76 forks source link

Question: How to aggregate several errors? #413

Closed jnussbaum closed 3 days ago

jnussbaum commented 2 weeks ago

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 😃

brunato commented 2 weeks ago

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')
jnussbaum commented 2 weeks ago

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