Reputation: 7423
Is there any pure Python way to validate XML files with XSD. My python version is 2.x (2.6/2.7) and the code is already using xml.dom.minidom
for XML parsing. There are so many answers on StackOverflow but most are using ElementTree or lxml.
Is there any implementation with xml.dom.minidom
?
Upvotes: 2
Views: 2368
Reputation: 7423
I couldn't find any thing using minidom, so I used lxml to validate xml against xsd
from lxml import etree
xmlschema_doc = etree.parse('schema.xsd')
xml_doc = etree.parse('my.xml')
xmlschema = etree.XMLSchema(xmlschema_doc)
if xmlschema.validate(xml_doc):
print 'Valid xml'
else:
print 'Invalid xml'
we can also use xmlschema.assertValid(xml_doc)
to raise assertion exception
Upvotes: 1
Reputation:
As the name suggests, miniDom is a library for interacting with the Document Object Model (DOM) API. As wikipedia states:
The Document Object Model (DOM) is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents.
It doesn't focus on the "validity" of the document, just that it is well-formed and able to be parsed and browsed.
As such, miniDom won't have any XML validation capability as this is beyond the scope of the DOM API.
Upvotes: 1