Read XSD using org.eclipse.xsd.util.XSDResourceImpl

I successfully read the XSD schema using org.eclipse.xsd.util.XSDResourceImpl and process all the contained XSD elements, types, attributes, etc.
But when I want to process a reference to an element declared in an imported schema, I get null as its type. The imported schemas seem not to be handled by XSDResourceImpl . Any idea?

  final XSDResourceImpl rsrc = new XSDResourceImpl(URI.createFileURI(xsdFileWithPath)); rsrc.load(new HashMap()); final XSDSchema schema = rsrc.getSchema(); ... if (elem.isElementDeclarationReference()){ //element ref elem = elem.getResolvedElementDeclaration(); } XSDTypeDefinition tdef = elem.getType(); //null for element ref 

Update:
I invalidated the imported XSD but received no exception. This means that he really does not understand. Is there a way to force the import of XSD along with the main one?

+5
source share
3 answers

There is one important trick for handling imports and includes automatically. You must use a ResourceSet to read the main XSD file.

 import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.xsd.util.XSDResourceFactoryImpl; import org.eclipse.xsd.util.XSDResourceImpl; import org.eclipse.xsd.XSDSchema; static ResourceSet resourceSet; XSDResourceFactoryImpl rf = new XSDResourceFactoryImpl(); Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", rf); resourceSet = new ResourceSetImpl(); resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_LOCATION, Boolean.TRUE); XSDResourceImpl rsrc = (XSDResourceImpl)(resourceSet.getResource(uri, true)); XSDSchema sch = rsrc.getSchema(); 

Then, before processing an element, attribute, or model group, you should use this:

 elem = elem.getResolvedElementDeclaration(); attr = attr.getResolvedAttributeDeclaration(); grpdef = grpdef.getResolvedModelGroupDefinition(); 
+1
source

Could you try something like this, manually enable the type:

 final XSDResourceImpl rsrc = new XSDResourceImpl(URI.createFileURI(xsdFileWithPath)); rsrc.load(new HashMap()); final XSDSchema schema = rsrc.getSchema(); for (Object content : schema.getContents()) { if (content instanceof XSDImport) { XSDImport xsdImport = (XSDImport) content; xsdImport.resolveTypeDefinition(xsdImport.getNamespace(), ""); } } 
0
source

You can look here . Partially in this method:

  private static void forceImport(XSDSchemaImpl schema) { if (schema != null) { for (XSDSchemaContent content: schema.getContents()) { if (content instanceof XSDImportImpl) { XSDImportImpl importDirective = (XSDImportImpl)content; schema.resolveSchema(importDirective.getNamespace()); } } } } 
0
source

Source: https://habr.com/ru/post/1242367/


All Articles