Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Validate a XSD Schema that imports another XSD Schema

843834Jul 22 2010 — edited Jul 22 2010
Hey!

I would like to validate XML reports with a XSD schema. I do it like this:
SchemaFactory schemaFactory = SchemaFactory
       .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Source schemaFile = new StreamSource(new File(MyClass.class
      .getClassLoader().getResource("mainschema.xsd")
      .toURI()));
Schema schema = schemaFactory.newSchema(schemaFile);
 schema.newValidator().validate(new DOMSource(ervBericht));
Everything works pretty well, also the importing of another xsd does not make any problems:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="..." xmlns:typ="..." targetNamespace="..." version="0.5.2" elementFormDefault="qualified">
  <xsd:import namespace="..." schemaLocation="another.xsd"/>
  ...
</xsd:schema>
But if I create an artifact JAR out of my validation to use it as a jar library in other projects, I cannot access my resources like this. Searching the web brought me to the solution to use this snippet to get to the resources inside the jar:
Source schemaFile = new StreamSource(MyClass.class
      .getClassLoader().*getResourceAsStream*("mainschema.xsd")
      .toURI());
I did that with my properties files and other plain text files - and it worked. But using StreamSource with an InputStream as parameter brings up the problem. Suddenly I get errors complaining that it cannot find schema types defined in the imported XSD:
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'typ:DienststelleMenge' to a(n) 'type definition' component.
	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
	at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDSimpleTypeTraverser.findDTValidator(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDSimpleTypeTraverser.getSimpleType(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDSimpleTypeTraverser.traverseSimpleTypeDecl(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDSimpleTypeTraverser.traverseGlobal(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown Source)
	at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
	at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
	at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
	at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
	at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
	at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:594)
	at at.gv.brz.sta.validator.rules.BR0301.validateErvBericht(BR0301.java:73)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at at.gv.brz.sta.validator.impl.ErvBerichtValidatorImpl.reflectRuleClass(ErvBerichtValidatorImpl.java:131)
	at at.gv.brz.sta.validator.impl.ErvBerichtValidatorImpl.validateErvBericht(ErvBerichtValidatorImpl.java:85)
	at at.gv.brz.sta.validator.test.BerichtSender.sendBericht(BerichtSender.java:62)
	at at.gv.brz.sta.validator.test.UnitTest.testValidBericht(UnitTest.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I cannot just copy those two XSD's together, I need to do it with the import tag.

Can anybody please help me?

regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 19 2010
Added on Jul 22 2010
1 comment
11,296 views