17

I have two xsd files to validate a xml. But the problem is my code takes only one xsd. How to use other xsd in the following code? I dont have idea about where should i place/call 2nd xsd file.

             private void validate(File xmlF,File xsd1,File xsd2) {
                    try {
                        url = new URL(xsd.toURI().toString());//  xsd1
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }


                    source = new StreamSource(xml); // xml
                    try {
                        System.out.println(url);
                        schema = schemaFactory.newSchema(url);
                    } catch (SAXException e) {
                        e.printStackTrace();
                    }
                    validator = schema.newValidator();
                    System.out.println(xml);
                    try {
                        validator.validate(source);
                    } catch (SAXException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
freepublicview
  • 716
  • 2
  • 12
  • 25
  • 4
    Did you already try [`newSchema(Source[])`](http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html#newSchema(javax.xml.transform.Source[]))? – Michael-O Aug 10 '11 at 10:42
  • Yes. I tried that way. Its not working , probably because Source[] is used for xml's. We cannot typecast xsd to source. – freepublicview Aug 10 '11 at 11:26
  • 2
    As with the previous question regarding this XML validation project, I'd like to point you to [SSCCE](http://sscce.org/). Your code snippet is far from complete as you define variables outside of this method etc. Putting attention to the way you ask your question, helps us helping you. – Wivani Aug 10 '11 at 11:51
  • 1
    Possible duplicate of [XML to be validated against multiple xsd schemas](https://stackoverflow.com/questions/2624763/xml-to-be-validated-against-multiple-xsd-schemas) – rogerdpack Feb 06 '19 at 15:43

1 Answers1

33

Plenty of hits when searching on SO or Google. One of them is this question, where the author found his own solution and reports the following code to add multiple xsd's to the validator:

Schema schema = factory().newSchema(new Source[] {
  new StreamSource(stream("foo.xsd")),
  new StreamSource(stream("Alpha.xsd")),
  new StreamSource(stream("Mercury.xsd")),
});

However, when working directly with InputStream on StreamSource, the resolver is not able to load any referenced XSD files. If, for instance, the file xsd1 imports or includes a third file (which is not xsd2), schema creation will fail. You should either set the system identifier (setSystemId) or (even better) use the StreamSource(File f) constructor.

Adjusted to your example code:

try {
  schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  schema = schemaFactory.newSchema(new Source[] {
    new StreamSource(xsd1), new StreamSource(xsd2)
  });
} catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

Note:

If working with classpath resources, I'd prefer the StreamSource(String systemId) constructor (rather than creating a File):

new StreamSource(getClass().getClassLoader().getResource("a.xsd").toExternalForm());
Line
  • 1,529
  • 3
  • 18
  • 42
Wivani
  • 2,036
  • 22
  • 28
  • This is exactly the same as my question above. – Michael-O Aug 11 '11 at 10:26
  • 6
    It is the same solution you offered in your comment. That's why I upvoted your comment. OP had trouble implementing it though; I had the code ready in my Eclipse from answering his previous question and simply adjusted it to show how to use Source[]. No hard feelings, I hope? – Wivani Aug 11 '11 at 11:18
  • @Michael-O that's why you should post it as answer, not comment – Line Oct 18 '17 at 08:24
  • 1
    `new StreamSource(getClass().getClassLoader().getResource("a.xsd").toExternalForm())` solved my problem, thx! – RoBeaToZ Mar 09 '20 at 15:56