How do I validate XML document via compact RELAX NG schema in Python?
+6
A:
How about using lxml?
From the docs:
>>> f = StringIO('''\
... <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
... <zeroOrMore>
... <element name="b">
... <text />
... </element>
... </zeroOrMore>
... </element>
... ''')
>>> relaxng_doc = etree.parse(f)
>>> relaxng = etree.RelaxNG(relaxng_doc)
>>> valid = StringIO('<a><b></b></a>')
>>> doc = etree.parse(valid)
>>> relaxng.validate(doc)
True
>>> invalid = StringIO('<a><c></c></a>')
>>> doc2 = etree.parse(invalid)
>>> relaxng.validate(doc2)
False
maksymko
2009-08-10 13:46:26
Thanks! Althought lxml does not support compact syntax, it's possible to convert it to xml with Trang http://www.thaiopensource.com/relaxng/trang.html
Epeli
2009-08-11 08:51:13