Do you have a xml a xml and want validate?
This is all that you need

require 'nokogiri'

xml_path = "/path/to/xml.xml"
xsd_path = "/path/to/xsd.xsd"

xsddoc = Nokogiri::XML(File.read(xsd_path), xsd_path)

xsd = Nokogiri::XML::Schema.from_document(xsddoc)

doc = Nokogiri::XML.parse File.read(xml_path)

if xsd.valid?(doc)
  return "ok"
else
  for x_error in xsd.validate(doc)
    puts x_error.message
  end
end
from lxml import etree

xml_path = "/path/to/xml.xml"
xsd_path = "/path/to/xsd.xsd"

xmlschema_doc = etree.parse(xsd_path)

xmlschema = etree.XMLSchema(xmlschema_doc)

doc = etree.parse(xml_path)

# if you use a xml string use:
# doc = etree.parse(StringIO(xml))

if xmlschema.validate(doc):
  print("ok")
else:
  for i in xmlschema.error_log: 
    print(i.message)
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

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 Validate {
  public static void main(String[] args) throws IOException, SAXException {
    String xml_path = "/path/to/xml.xml";
    String xsd_path = "/path/to/xsd.xsd";

    List<String> errors = vai(xml_path, xsd_path);
    if (errors.size() > 0) {
      for (String string : errors) {
        System.out.println(string);
      }
    } else {
      System.out.println("ok");
    }
  }

  public static List<String> vai(String xml_path, String xsd_path) throws IOException, SAXException {
    String schemaLang = "http://www.w3.org/2001/XMLSchema";
    SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

    Schema schema = factory.newSchema(new StreamSource(xsd_path));

    Validator validator = schema.newValidator();

    List<String> errors = new LinkedList<String>();

    validator.setErrorHandler(new ErrorHandler() {
      @Override
      public void warning(SAXParseException e) throws SAXException {
        errors.add(e.getMessage());
      }

      @Override
      public void fatalError(SAXParseException e) throws SAXException {
        errors.add(e.getMessage());
      }

      @Override
      public void error(SAXParseException e) throws SAXException {
        errors.add(e.getMessage());
      }
    });
    
    // if you use a xml string use:
    // validator.validate(new StreamSource(new StringReader(xml)));
    validator.validate(new StreamSource(xml_path));

    return errors;
  }
}

🗓 2016-12-12
820 👀