Validate XML with loading schemas at runtime, failure depending on schema order
Asked Answered
C

2

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?

Collogue answered 7/8, 2012 at 18:55 Comment(2)
Can you post the schemas and XML somewhere?Oira
@Oira 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.Collogue
W
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

Weightless answered 7/8, 2012 at 20:16 Comment(6)
I am getting an HTTP 404 in this link, could you provide some additional details?Collogue
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.Collogue
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).Weightless
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 recognizedCollogue
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!Weightless
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.Collogue
S
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.

Sampan answered 28/8, 2012 at 9:49 Comment(1)
Class is com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactoryCollogue

© 2022 - 2024 — McMap. All rights reserved.