Skip to main content

SchemaFactory - Problem with compiling multiple schemas in same namespace

6 replies [Last post]
chaganthi
Offline
Joined: 2007-06-05
Points: 0

Hello
Till now, I have been able to use the SchemaFactory to validate a single schema file against xml source. No problems.

Now, I have a new schema that is actually broken up into a number of schemas. All belong to same namespace and the main schema "includes" all the others.

So, I have "main.xsd" that inturn includes "1.xsd", ...... "n.xsd".

I tried
1) factory.newSchema(new StreamSource(...."main.xsd"));
2) factory.newSchema(sources); // sources is an array of Source

All of them fail with the dreaded
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'someElement' to a(n) 'element declaration' component.

If I were to append all my xsd into main.xsd it works just fine.

The xsd:include tags within main.xsd refers to all the other xsds with just the relevant name. No relative paths or anything like that.

Does anyone have a suggestion/solution to this problem? Any pointers are much appreciated.

I have seen a similar bug at
http://issues.apache.org/jira/browse/XERCESJ-1130
but no workaround. Is it still an issue or has anyone been able to get around this problem by custom coding LSResourceResolver?

Thanks
Madhu

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
djvano
Offline
Joined: 2005-05-05
Points: 0

Hello chaganthi,

I'm facing to the same problem you described... At this time I don't still know how to implement the 'LSResourceResolver'.

Could you provide us a snippet of your code ?

Thanks in advance,

Sébastien

justinrowe
Offline
Joined: 2009-10-08
Points: 0

Here's a code example that shows how to load imported xsd schemas from the classpath:

...

private Schema createSchema(String schemName)
throws IOException, SAXException, ClassNotFoundException, InstantiationException, IllegalAccessException {
// create a SchemaFactory
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
[b]factory.setResourceResolver(new ClasspathResourceResolver());[/b]

// locate the schema on the classpath
InputStream schemaStream = getClass().getResourceAsStream("/" + schemName);
if (schemaStream == null) {
throw new IOException("XML schema not found on classpath: " + schemName);
}

return factory.newSchema(new StreamSource(schemaStream));
}

/**
* This is an implementation of LSResourceResolver that can resolve XML schemas from the classpath
*/
[b]private class ClasspathResourceResolver implements LSResourceResolver[/b] {
private DOMImplementationLS domImplementationLS;

private ClasspathResourceResolver() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
}

public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId,
String baseURI) {
logger.info(
"==== Resolving '" + type + "' '" + namespaceURI + "' '" + publicId + "' '" + systemId + "' '" +
baseURI + "'");

LSInput lsInput = domImplementationLS.createLSInput();
InputStream is = getClass().getResourceAsStream("/" + systemId);
lsInput.setByteStream(is);
lsInput.setSystemId(systemId);
return lsInput;
}
}

chaganthi
Offline
Joined: 2007-06-05
Points: 0

I am answering this myself.
(;-)

Implementing a custom LSResourceResolver and supplying schemas to SchemaFactory via a Source[] did the trick.

All xsds have been jarred with a custom manifest (that had class-path entries) and that jar has been placed in the WEB-INF/lib folder.

Hopefully it will help others that might run into this issue.

Thanks
Madhu

johnlyng
Offline
Joined: 2007-06-26
Points: 0

Hi.
I have the same problem.
How do implement the LSresolver?

could you give an example..

djvano
Offline
Joined: 2005-05-05
Points: 0

Actually facing also this problem... Any example on how to implement this famous 'LSResourceResolver' interface ??

Thanks in advance,

Séba

chaganthi
Offline
Joined: 2007-06-05
Points: 0

Other interesting observations.

If I were to have all my xsds in %JBoss_Home%\bin directory, there are no problems.I would not want that to be my solution for obvious reasons. The schema gets compiled and validated.

I tried jarring all the xsds with a class-path manifest and using that jar. Did not work.

Tried having all xsds within my web-inf/classes directory, that did not work either.

Thanks
Madhu