Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating with an XML schema in Python

I have an XML file and an XML schema in another file and I'd like to validate that my XML file adheres to the schema. How do I do this in Python?

I'd prefer something using the standard library, but I can install a third-party package if necessary.

like image 599
Eli Courtwright Avatar asked Nov 18 '08 17:11

Eli Courtwright


People also ask

How do I check if an XML file is valid in Python?

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. The method raises an exception if the file doesn't validate against the XSD.

Can we validate XML schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported.

How do you validate an XML string in Python?

Just try to parse it with ElementTree (xml. etree. ElementTree. fromstring) - it will raise an error if the XML is not well formed.


1 Answers

I am assuming you mean using XSD files. Surprisingly there aren't many python XML libraries that support this. lxml does however. Check Validation with lxml. The page also lists how to use lxml to validate with other schema types.

like image 71
Keegan Carruthers-Smith Avatar answered Sep 25 '22 04:09

Keegan Carruthers-Smith