You can easily validate an XML file or tree against an XML Schema (XSD) with the xmlschema Python package. It's pure Python, available on PyPi and doesn't have many dependencies.
Example - validate a file:
import xmlschema
xmlschema.validate('doc.xml', 'some.xsd')
The method raises an exception if the file doesn't validate against the XSD. That exception then contains some violation details.
If you want to validate many files you only have to load the XSD once:
xsd = xmlschema.XMLSchema('some.xsd')
for filename in filenames:
xsd.validate(filename)
If you don't need the exception you can validate like this:
if xsd.is_valid('doc.xml'):
print('do something useful')
Alternatively, xmlschema directly works on file objects and in memory XML trees (either created with xml.etree.ElementTree or lxml). Example:
import xml.etree.ElementTree as ET
t = ET.parse('doc.xml')
result = xsd.is_valid(t)
print('Document is valid? {}'.format(result))