Validating XML with Python's LXML package
The most commonly used Python package IMO for managing (and parsing) XML is lxml
(https://lxml.de/), but my experience with it for validating an XML instance document is limited. The basics are as follows (and you mileage may vary):
from lxml import etree
# XML DOM instance of EML document
xml_file = etree.parse("EML.xml")
# XML Schema validator instance for EML 2.2.0 schema - assumes local copy of schema
xml_validator = etree.XMLSchema(file="./eml-2.2.0/xsd/eml.xsd")
# Validate method returns a boolean
is_valid = xml_validator.validate(xml_file)
if is_valid:
print("EML document is valid")
else:
print("EML document is not valid")
To see more details about the validation exception, you can substitute assertValid
for the validate
method.
Perhaps the best introduction to lxml
is John Shipman's "Python XML processing with lxml".