How can I make Python's ElementTree enforce XML schema?
python, xml
Solution
ElementTree from the STD lib has not schema support. For this, I suggest you to use the lxml package which has it (and by the way, it's much faster).
Here after an example from my own code:
from lxml import etree
# Create the schema object
with open(xsd_file) as f:
xmlschema_doc = etree.parse(f)
xmlschema = etree.XMLSchema(xmlschema_doc)
# Create a tree for the XML document
doc = etree.parse(xml_text)
# Validate the XML document using the schema
return xmlschema.validate(doc)
or if you want a exception to be raised:
xmlschema.assertValid(doc)
Problem
Suppose I wish to parse an XML document, and its schema dictates that a given element can only occur once. How do I make sure that an exception gets raised if the element occurs twice or more? Or, if the schema says that a given element's value should be an integer, and the value is "turkey sandwich", how do I make the parser crash and burn like it's supposed to? Can ElementTree do this? Can anything do this? Does this question even make sense?