The Source for Java Technology Collaboration

Home » java.net Forums » Java Web Services and XML » JAXP

Thread: SchemaFactory - Problem with compiling multiple schemas in same namespace

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is answered. Helpful answers available: 2. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 6 - Last Post: Oct 8, 2009 4:13 AM by: justinrowe Threads: [ Previous | Next ]
chaganthi

Posts: 3
SchemaFactory - Problem with compiling multiple schemas in same namespace
Posted: Jun 5, 2007 12:22 PM
 
  Click to reply to this thread Reply

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.
<xsd:include schemaLocation="1.xsd">

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

chaganthi

Posts: 3
Re: SchemaFactory - Problem with compiling multiple schemas in same namespa
Posted: Jun 5, 2007 12:36 PM   in response to: chaganthi
 
  Click to reply to this thread Reply

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

chaganthi

Posts: 3
Re: SchemaFactory - Problem with compiling multiple schemas in same namespa
Posted: Jun 6, 2007 8:22 AM   in response to: chaganthi
 
  Click to reply to this thread Reply

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

Posts: 2
Re: SchemaFactory - Problem with compiling multiple schemas in same namespa
Posted: Jun 26, 2007 1:40 AM   in response to: chaganthi
 
  Click to reply to this thread Reply

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

could you give an example..

djvano

Posts: 2
Re: SchemaFactory - Problem with compiling multiple schemas in same namespa
Posted: Aug 14, 2008 6:09 AM   in response to: johnlyng
 
  Click to reply to this thread Reply

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

Thanks in advance,

Séba

djvano

Posts: 2
Re: SchemaFactory - Problem with compiling multiple schemas in same namespa
Posted: Aug 22, 2008 6:08 AM   in response to: chaganthi
 
  Click to reply to this thread Reply

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

Posts: 1
Re: SchemaFactory - Problem with compiling multiple schemas in same namespa
Posted: Oct 8, 2009 4:13 AM   in response to: djvano
 
  Click to reply to this thread Reply

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");
factory.setResourceResolver(new ClasspathResourceResolver());

// 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
*/
private class ClasspathResourceResolver implements LSResourceResolver {
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;
}
}




 XML java.net RSS