1

I am trying to validate a XML file against many XSD files. At this moment I can only manually enter the xsd schema. ( I mean that I check my XML file and look at the XSD definition). How can I detect the XSD schema files in any of the XML file?

What I tried so far: // Following method check the xml file against multiple xsd schema files.

public static void XSDValidation() throws SAXException, IOException, ParserConfigurationException {

    try {

        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        Schema sch = schemaFactory.newSchema(new Source[] { new StreamSource(new FileInputStream("role-1.2.xsd")),
                new StreamSource(new FileInputStream("role-1.1.xsd")) });

        Validator validator = sch.newValidator();

        validator.validate(new StreamSource("role.xml"));

        System.out.println("INFO: Validation is done successfully.");

    } catch (Exception e) {
        System.out.println("ERROR: Validation is failed.");
    }
}

I use DOM Parser and Java 8.

Advance thanks for all help.

EDIT: @ kjhughes I did not simply ask

What's the best way to validate an XML file against an XSD file?

My question: how can we detect the XSD schema files in a XML file. For instance when I want to validate a.xml which includes b.xsd and c.xsd, I want to detect these files automatically and in a true order. I mean that if the XML use firstly the namespace from c.xsd then it should be detected. In my example the XSD schema files and their order were written manually by checking inside of XML.

limonik
  • 499
  • 1
  • 6
  • 28

1 Answers1

1

You can use LSResourceResolver to resolve the schema dependencies. Although you have to specify the all the dependencies in here, it isolates the code from the schema references.

schemaFactory.setResourceResolver(new LSResourceResolver(){
    @Override
    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
            // resovle the path to schema , read and return it.
    }

});
ulab
  • 1,079
  • 3
  • 15
  • 45