Reputation: 79
I'm generating e-invoicing XML files (CII D16B) using Python. These files comply with the EN16931 standard (https://github.com/ConnectingEurope/eInvoicing-EN16931).
I need to validate the generated XML against the schematron rules provided in the ConnectingEurope repository (eInvoicing-EN16931).
I'm having trouble finding information on performing schematron validation in Python.
I've tried using libraries like lxml, xmlschema, and saxonc (potentially requiring a license) along with the built-in xml library, but haven't been successful.
The complexity lies in the XML structure: it uses four different namespaces with intricate nested rules defined within a global schematron file (https://github.com/ConnectingEurope/eInvoicing-EN16931/blob/master/cii/schematron/EN16931-CII-validation.sch).
While online validation tools exist (e.g., https://ecosio.com/en/peppol-and-xml-document-validator/, https://invoice-portal.de/en/peppol-bis-xrechnung-validator/), I'd prefer to integrate validation within my Python script before generating the final XML.
Does anyone have experience with schematron validation in Python? Are there specific libraries or approaches recommended for this scenario, especially considering the multiple namespaces and nested rules?
Any guidance or code examples would be greatly appreciated!
Thanks !
Upvotes: 2
Views: 620
Reputation: 636
That's how you can do it with Python and lxml package. When you say you tried lxml is that how you did it?
from lxml import etree, isoschematron
def validate_with_schematron(message:str,schematron:str):
xml_message = etree.fromstring(message)
parsed_schematron = etree.fromstring(schematron)
validator = isoschematron.Schematron(parsed_schematron,store_report = True)
is_valid = validator(xml_message)
result = validator.validation_report
return {"is_valid":is_valid, "validation_report": result}
Upvotes: 0