MOXY- Multiple XSDs import the same schema definit

2019-07-30 01:09发布

问题:

I have 3 XSDs linked together:

schema1.xsd
    imports namespace="x:y:z" schemaLocation= "schemaDefinitions.xsd"
    includes schema2.xsd
schema2.xsd
    imports namespace="x:y:z" schemaLocation= "schemaDefinitions.xsd"
    includes schema3.xsd
schema3.xsd
    imports namespace="x:y:z" schemaLocation= "schemaDefinitions.xsd"

These xsds are provided by an outside source and cannot be changed.

Previously my project used standard JAXB with classes created at compile time. I currently am switching to Dynamic JAXB MOXY (runtime) and now receive the following error on my DynamicJAXBContextFactory.createContextFromXSD() line, which uses schema1.xsd for FileInputStream:

Exception in thread "main" java.lang.ExceptionInInitializerError at 
    TestTool.JavaRoot.TestTools.MainTool.main(MainTool.java:55)
    Caused by: Exception [EclipseLink-50040] (Eclipse Persistence Services - 
    2.6.2.v20151217-774c696): 
    org.eclipse.persistence.exceptions.JAXBException
Exception Description: Error creating DynamicJAXBContext.
    Internal Exception: org.xml.sax.SAXParseException; systemId: 
    file:///public/SITE1/config/schema/SchemaDefinitions.xsd; lineNumber: 
    xyz; columnNumber: xyz; 'xyz' is already defined

I have determined the cause is the fact that all three schemas import schemaDefinitions.xsd. If I remove the import statement from schema2 and schema3 the error is resolved. This error was not present with the previous implementation of jaxb and the xsds have not changed since switching to MOXY.

Questions:

  1. Is it legal/ valid for the xsds to import/ include in this way

  2. What are possible work arounds since I can not modify the XSDs? Perhaps modifications to the bindings xjb file?

回答1:

Another answerer may be able to help directly with any MOXy configuration support in the area of duplicate declarations, but at purely the XSD level:

  1. Unfortunately the W3C XSD Recommendation allows implementation-dependent behavior to occur when an XSD is imported multiple times. (See last note in 4.2.3 References to schema components across namespaces)
  2. Depending upon the underlying XSD processor upon which MOXy is built, you may be able to set a flag to allow/disallow multiple imports. For Xerces, see honour-all-schemaLocations; for Saxon, see multipleSchemaImports.1

See also Is it an error to import the same XSD multiple times?


1 Note a pending improvement on the semantics of multipleSchemaImports.



回答2:

Resolved issue by turning off error check with putting the following line in MyEntityResolver.java class:

System.setProperty("com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.noCorrectnessCheck", "true");

I had tried this in my main java class before, apparently thats the wrong place for it!