2

I need to edit a xml-schema file (.xsd) within an eclipse plugin project (eclipse version is 4.2.2). Right now, I'm able to read the xsd IFile in my eclipse project using org.apache.xerces.xs.XSModel in a way like this:

private XSModel readXSDModelFromResource(IFile xsdFile) throws CoreException, ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException {

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    XSImplementation xsImplementation = (XSImplementation) registry.getDOMImplementation("XS-Loader");
    XSLoader schemaLoader = xsImplementation.createXSLoader(null);

    LSInput input = new DOMInputImpl();
    input.setBaseURI(xsdFile.getLocation().removeLastSegments(1).addTrailingSeparator().toOSString());
    input.setSystemId(xsdFile.getFullPath().lastSegment());
    input.setByteStream(xsdFile.getContents());

    XSModel model = schemaLoader.load(input);

    return model;
}

This is fine! My XSModel has the correct representation of the XML Schema, also following all the xsd:include tags.

But, I read here that

XSModel: a read-only interface that represents an XML Schema, which could be components from different namespaces.

And here that

XMLSchema is a lightweight Java object model that can be used to manipulate and generate XML schema representations. You can use it to read XML Schema (xsd) files into memory and analyze or modify them, or to create entirely new schemas from scratch.

Hence, since my needs are to add or delete elements, changing target namespaces and so on, my doubts are:

  • how can I manipulate the XSModel? What is (if exists) the related read-write object/library?
  • if i have to use Apache XMLSchema library, how can I import it into eclipse plugin project since the dependecies list for plugin.xml have nothing related to org.apache.ws.* package?

I'd like to avoid, if possile (I hope so :) ), the raw xml dom manipulation with DOM, SAX, DOM4J....

Thanks to all

  • I also read about EMF (Eclipse Modelling Framework) to deal with XSD editing. Can anyone provide some sample how to achieve this? Thanks – Sergio Panico Jun 13 '20 at 14:28
  • 1
    I can confirm that the EMF XSD model is a very mature and robust impementation of the entire XSD specification. It's well suited to the job of loading an entire XSD model, modifying it programmatically and writing out a new version. It's a bit scary at first, but the API is well designed and consistent so you get the hang of it quite quickly. Sorry that I can't supply a sample - it's been a long time since I worked with it. – kimbert Jun 13 '20 at 21:40
  • Do you reref to this: https://wiki.eclipse.org/MDT-XSD-FAQ, don't you? I'll try this API. Hope this will work for me! – Sergio Panico Jun 13 '20 at 22:01
  • yes, that's the project that I'm referring to. – kimbert Jun 14 '20 at 23:02

0 Answers0