radai
radai

Reputation: 24202

Jaxb xsd generation producing 2 *.xsd's

i have the following 2 classes:

@XmlRootElement(namespace = "namespaceX")
@XmlSeeAlso({SubClass.class})
public class BaseClass {
    private String baseProp;

    @XmlAttribute
    public String getBaseProp() {
        return baseProp;
    }

    public void setBaseProp(String baseProp) {
        this.baseProp = baseProp;
    }
}

@XmlRootElement(namespace = "namespaceX")
public class SubClass extends BaseClass {
    private String extraProp;

    @XmlElement
    public String getExtraProp() {
        return extraProp;
    }

    public void setExtraProp(String extraProp) {
        this.extraProp = extraProp;
    }
}

and im trying to generate an *.xsd out of them:

JAXBContext context = JAXBContext.newInstance(BaseClass.class);
final List<DOMResult> results = new ArrayList<DOMResult>();
context.generateSchema(new SchemaOutputResolver() {
    @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
        // save the schema to the list
        DOMResult result = new DOMResult();
        result.setSystemId( suggestedFileName );
        results.add( result );
        return result;
    }
});

for (DOMResult domResult : results) {
    Document doc = (Document) domResult.getNode();
    OutputFormat format = new OutputFormat(doc);
    format.setIndenting(true);
    StringWriter writer = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(writer, format);
    serializer.serialize(doc);

    String xsd = writer.toString();

    System.err.println("xsd "+domResult.getSystemId()+":");
    System.err.println(xsd);
}

im expecting to see a single xsd targeting "namespaceX". instead im getting 2:

xsd schema1.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="namespaceX" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="schema2.xsd"/>
    <xs:element name="baseClass" type="baseClass"/>
    <xs:element name="subClass" type="subClass"/>
</xs:schema>

xsd schema2.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="baseClass">
        <xs:sequence/>
        <xs:attribute name="baseProp" type="xs:string"/>
    </xs:complexType>
    <xs:complexType name="subClass">
        <xs:complexContent>
            <xs:extension base="baseClass">
                <xs:sequence>
                    <xs:element minOccurs="0" name="extraProp" type="xs:string"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

what im i doing wrong?

Upvotes: 1

Views: 143

Answers (1)

bdoughan
bdoughan

Reputation: 149047

Instead of specifying the namespace on each of the @XmlRootElement annotations, I would map the namespace qualification using the package level @XmlSchema annotation on the package-info class.

If only the global elements should be namespace qualified then elementFormDefault is UNQUALIFIED, if all elements should be namespace qualified then specify QUALIFIED.

@XmlSchema(
    namespace = "namespaceX",
    elementFormDefault = XmlNsForm.UNQUALIFIED)
package your_package;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

For More Information

I have written more about JAXB and namespace qualification on my blog:

Upvotes: 1

Related Questions