MOXy does not import other schemas that it generates

I follow the instruction here in order to generate the XML Schema for my annotated classes. I am using MOXy for the underlying implementation of JAXB because I need its certain features like @XmlPath and how it handles the primitive types for XmlAdapter.

The code in the link above generates schema into two schema files, i.e. one contains the rules for simple and complex types and the other one contains only the schema for the enum types. Ideally, I want them to be in a single schema file but that is not the main issue for now.

When I use the Sun implementation of JAXB, it generates the schema and imports the other one. However, when I switch to use MOXy implementation, it does not import the other schema, see the example below:

<xsd:schema xmlns:ns0="http://my/schema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://my/schema" elementFormDefault="qualified">
    <!-- this bit is missing when MOXy is used -->
    <xsd:import schemaLocation="schema2.xsd"/>
...
</xsd:schema>

Does anyone know how to solve this problem?

Here is the bit of code I used:

    JAXBContext context = JAXBContext.newInstance(Person.class, IdentifierType.class);
    SchemaOutputResolver sor = new MySchemaOutputResolver();
    context.generateSchema(sor);

Person is a standard POJO and IdentifierType is enum. They are both annotated with JAXB annotations, like @XmlRootElement, @XmlEnum, etc. (I could provide more codes if needed)

Answer

UPDATE – ISSUE FIXED

Thank you for entering the followng bug for this issue.

This has now been fixed in the EclipseLink 2.4.2 and 2.5.0 streams. You can try out the fix using a nightly download starting February 12th, 2013 from think below:


I have not been able to recreate the issue that you are seeing. Below is what I have tried so far.

SCENARIO #1 – Person and IdentifierType in Same Namespace

If the POJO and the Enum are in the same namespace then only one XML schema will be generated.

Person (POJO)

package forum14778338;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

    private IdentifierType identifierType;

    public IdentifierType getIdentifierType() {
        return identifierType;
    }

    public void setIdentifierType(IdentifierType identifierType) {
        this.identifierType = identifierType;
    }

}

IdentifierType

package forum14778338;

public enum IdentifierType {

    FOO,
    BAR

}

jaxb.properties

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

package forum14778338;

import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceURI, String suggestedFileName)
                    throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        });
    }

}

Output

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="person">
      <xsd:sequence>
         <xsd:element name="identifierType" type="identifierType" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="person" type="person"/>
   <xsd:simpleType name="identifierType">
      <xsd:restriction base="xsd:string">
         <xsd:enumeration value="FOO"/>
         <xsd:enumeration value="BAR"/>
      </xsd:restriction>
   </xsd:simpleType>
</xsd:schema>

SCENARIO #2 – Person and IdentifierType in Different Namespaces

If the POJO and Enum are in different namespaces then two XML schemas should be generated.

IdentifierType

I have added the @XmlType annotation to the IdentifierType enum to put it in a different namespace.

package forum14778338;

import javax.xml.bind.annotation.*;

@XmlEnum
@XmlType(namespace="foo")
public enum IdentifierType {

    FOO,
    BAR

}

Output

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:ns0="foo" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:import schemaLocation="schema2.xsd" namespace="foo"/>
   <xsd:complexType name="person">
      <xsd:sequence>
         <xsd:element name="identifierType" type="ns0:identifierType" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="person" type="person"/>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:ns0="foo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="foo">
   <xsd:simpleType name="identifierType">
      <xsd:restriction base="xsd:string">
         <xsd:enumeration value="FOO"/>
         <xsd:enumeration value="BAR"/>
      </xsd:restriction>
   </xsd:simpleType>
</xsd:schema>

Leave a Reply

Your email address will not be published. Required fields are marked *