6

I am trying to do xml validation. I am being given a list of schemas at run-time (possibly wrapped in a jar). Validation passes or failes based on the order in which I provide the schemas to the SchemaFactory.

Here is what I am doing:

  private void validateXml(String xml, List<URI> schemas){
        Source[] source = new StreamSource[schemas.size()];
        int i=0;
        for (URI f : schemas){
           source[i++] = new StreamSource(f.openStream());
        }

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NA_URI);
        sf.setResourceResolver(new MyClassPathResourceResolver());

        Schema schema = schemaFactory.newSchema(source);
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes()));

again, this fails if the passed set of schema do not start with the schema to which the root element of the xml referrs. Is there a fix to this or am I doing something wrong?

John B
  • 32,493
  • 6
  • 77
  • 98
  • Can you post the schemas and XML somewhere? – davidfmatheson Aug 22 '12 at 12:46
  • @davidfmatheson Unfortunately that is not possible, I can say that it is a set of schemas, the root schema and a second schema that allows for the replacement of the body of the first schema with a different tag. – John B Aug 22 '12 at 12:54

2 Answers2

5

By default Xerces will ignore a schema document if it already has a schema document for the same namespace. This behaviour can be changed using the factory option

http://apache.org/xml/features/validation/schema/handle-multiple-imports

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I am getting an HTTP 404 in this link, could you provide some additional details? – John B Aug 08 '12 at 12:12
  • Also, I need to load 2 schemas (each of which import additionals schemas). If I load these in the correct order, all's well. Otherwise, no joy. The two schemas have different target namspaces. – John B Aug 08 '12 at 12:13
  • Try searching for it instead of using it as a URL. It's a JAXP option name not a URL. (OK, some browsers make that difficult by combining the address bar and the search bar. Do it the old way by going to google.com). – Michael Kay Aug 10 '12 at 19:46
  • When I attempt to set the above feature on the factory I get: `SAXNotRecognizedException: Feature "http://apache.org/xml/features/validation/schema/handle-multiple-imports" is not recognized` – John B Aug 22 '12 at 12:30
  • Perhaps you're using the JDK version of Xerces rather than the Apache version? But sorry, I've pointed you in the right direction, but I can't go any further than this is providing support for my competitors' products! – Michael Kay Aug 23 '12 at 20:44
  • The class of the SchemaFactory is: `com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory`, so this IS apache, right? With it (using version 2.0.2 of `xml-apis`) I still get the `SAXNotRecognizedException` for the above feature. – John B Aug 28 '12 at 15:51
0

Firstly, you must set an instance of org.xml.sax.ErrorHandler object on XML reader by calling registerErrorHandler() method. You might receive warnings which would give you clue about issue.

Secondly, you must know which xml library you are using. Call schemaFactory.getClass().getName() in your code and print it. Once you know library, you can refer its documentation if it supports feature to turn on/off multiple schema imports.

ag112
  • 5,537
  • 2
  • 23
  • 42