views:

165

answers:

1

Hi all,

I was just wondering if someone could give my XML validation code a once over to see if I'm doing it right. Here's the portion of code that is giving me the trouble...

SAXParserFactory factory = SAXParserFactory.newInstance();

SchemaFactory schemaFactory = SchemaFactory
    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// *** CODE FAILS ON THE BELOW LINE **/
factory.setSchema(schemaFactory
    .newSchema(new Source[] { new StreamSource(schemaStream) }));

SAXParser parser = factory.newSAXParser();

SAXReader reader = new SAXReader(parser.getXMLReader());
reader.setValidation(false);
reader.setErrorHandler(new ResultProducingErrorHandler());
reader.read(content);

Whenever I run the above code, I get an error along the lines of:

src-resolve: Cannot resolve the name 'ns:myStructure' to a(n) 'type definition' component.

The elements mentioned in the error messages are all ones that are imported into the schema via calls to <xs:import />. The schema seems to validate OK via the W3C XML Schema Validator.

Do I have to include each of these schema's individually or is Java smart enough to go off and fetch these extra schema's too? I tried adding them in the array passed to the newSchema call but that didn't make any difference.

I don't think I can give out the link to the schema, so I'm really just looking for a yes or no regarding if my code looks at least acceptable.

+1  A: 

Ensure that the xs:import statements point to paths that are reachable from the current directory of your application. The current directory may not be what you think it is.

Eddie
The paths are just file names only (e.g. "abc.xsd"). These files live in the same directory as the original schema so I would have thought they would have been accessible. But then I don't know much about schemas and importing.
Lee Theobald
Just as a test, try supplying the whole path to the imported files in the importing schema.
Eddie
Should have tried that before I posted. With full paths, they work fine. So I guess ws:import isn't relative to the schema calling the import. Well at least that means it's the schema (which needs to be access over the web) and not me then. Cheers for the help.
Lee Theobald
@Lee Theobald: There may be a setting you can pass to the schema factory to tell it where to find imports.
Eddie
You can set a resource resolver on the schema factory.
McDowell