Jaxb xsd代产生2 * .xsd

时间:2014-06-09 08:41:28

标签: java xml jaxb

我有以下两个班级:

@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;
    }
}

我试图从中生成* .xsd:

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);
}

我希望看到单个xsd定位&#34; namespaceX&#34;。相反,我得到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>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我不会在每个@XmlRootElement注释上指定命名空间,而是使用@XmlSchema类上的包级别package-info注释来映射命名空间限定。

如果只有全局元素应该是名称空间限定的,那么elementFormDefaultUNQUALIFIED,如果所有元素都应该是名称空间限定的,那么请指定QUALIFIED

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

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

了解更多信息

我在博客上写了更多关于JAXB和名称空间限定的内容: