我正在使用 Python 生成电子发票 XML 文件 (CII D16B)。这些文件符合 EN16931 标准 (https://github.com/ConnectingEurope/eInvoicing-EN16931)。
我需要根据 ConnectingEurope 存储库 (eInvoicing-EN16931) 中提供的 schematron 规则验证生成的 XML。
我无法找到有关在 Python 中执行 schematron 验证的信息。
我尝试过使用 lxml、xmlschema 和 saxonc(可能需要许可证)等库以及内置 xml 库,但没有成功。
复杂性在于 XML 结构:它使用四个不同的命名空间,并在全局 schematron 文件中定义复杂的嵌套规则(https://github.com/ConnectingEurope/eInvoicing-EN16931/blob/master/cii/schematron/EN16931- CII-validation.sch).
虽然存在在线验证工具(例如,https://ecosio.com/en/peppol-and-xml-document-validator/、https://invoice-portal.de/en/peppol-bis-xrechnung -validator/),我更喜欢在生成最终的 XML 之前将验证集成到我的 Python 脚本中。
有人有 Python 中 schematron 验证的经验吗?是否有针对此场景推荐的特定库或方法,特别是考虑到多个命名空间和嵌套规则?
任何指导或代码示例将不胜感激!
谢谢!
这就是使用 Python 和 lxml 包来做到这一点的方法。当你说你尝试过 lxml 时,你是怎么做到的?
from lxml import etree
def validate_with_schematron(message:str,schema:str):
xml_schema = etree.XML(schema)
xml_message = etree.XML(message)
xmlSchema = etree.XMLSchema(xml_schema)
isValid = xmlSchema.validate(xml_message)
if not isValid == True:
for error in enumerate(xmlSchema.error_log):
print(error.message)
如果您可以发布 xml 和 schematron 文件的匿名示例,这可能有助于得出更好的答案