Sample code required to validate a xml file against XSD

Frank Millman frank at chagford.com
Fri Oct 3 01:19:39 EDT 2008


On Oct 2, 3:32 pm, hrishy <hris... at yahoo.co.uk> wrote:
> Hi
>
> Does anybody have a python xample program to validate a xml file against a XSD.
>
> regards
> Hrisy

I have used minixsv successfully in a test environment -

http://www.familieleuthe.de/MiniXsv.html

Here is some sample code -

#---------------------------------
from minixsv import pyxsval as xsv
try:
    etw = xsv.parseAndValidateXmlInput(
        'bpmn1.xml',
        'bpmn.xsd',
        xmlIfClass=xsv.XMLIF_ELEMENTTREE)
    et = etw.getTree()
    root = et.getroot()
except xsv.XsvalError,errstr:
    print errstr
#---------------------------------

It supports minidom, 4DOM, and ElementTree.

It is written in pure python. It is not very fast - around 0.5 sec for
a small xml file - but I have not compared it with any others, so I
don't know what is typical.

I believe that lxml also handles xsd files, but I have not tried it,
so I can't give any details.

HTH

Frank Millman



More information about the Python-list mailing list