[Python] – XML Validator against XML Schema (XSD)


This is the code:

from easyxsd import *
import sys

# Load XML Schema (.xsd file)
xsd = xsd_from_file(sys.argv[1])

# Load XML File
xml = xml_from_file(sys.argv[2])

# Validate
list_errors = validate_with_errors(xml, xsd)

# Open output file (validation response)
f = open(sys.argv[3],'w')

for error in list_errors:
    f.write(str(error) + '\n')


mime_exeDownload here an executable version for Windows

To execute:
validate_xml.exe C:\Documents\MyFolder\schema.xsd C:\Documents\MyFolder\myfile.xml C:\Documents\MyFolder\outputvalidate.txt

The shortcut works too:
validate_xml.exe schema.xsd myfile.xml outputvalidate.txt



2 thoughts on “[Python] – XML Validator against XML Schema (XSD)

  1. //
    // Segue codigo JAVA “semelhante”

    package br.com.newsoftware.rps.v3.client;

    import java.io.File;

    import javax.xml.XMLConstants;
    import javax.xml.transform.Source;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;

    import org.xml.sax.ErrorHandler;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;

    public class XMLValidation {

    public static void main(String[] args) {

    String pathBase = “C:\\workspace\\home\\criar\\RPS\\v3\\x\\”;
    String fileName = “a2.xml”;

    String xsd_file1 = “tipos_v03.xsd”;
    String xsd_file2 = “servico_enviar_lote_rps_envio_v03.xsd”;
    String xsd_file3 = “servico_enviar_lote_rps_resposta_v03.xsd”;
    try {
    Source[] sourceSchema = new StreamSource[] {
    new StreamSource(new File(pathBase + xsd_file1)),
    new StreamSource(new File(pathBase + xsd_file2)),
    new StreamSource(new File(pathBase + xsd_file3))
    // Append more and more…

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = factory.newSchema(sourceSchema);

    Validator validator = schema.newValidator();

    ForgivingErrorHandler errorHandler = new ForgivingErrorHandler();

    validator.validate(new StreamSource(new File(pathBase + fileName)));

    } catch (Exception e) {

    class ForgivingErrorHandler implements ErrorHandler {
    public void warning(SAXParseException ex) {

    public void error(SAXParseException ex) {

    public void fatalError(SAXParseException ex) throws SAXException {
    throw ex;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s