谢谢你的回复,布莱斯。问题似乎是由于我试图通过指定 propOrder = {} 来生成 xsd:all。使用该注释 MOXy 不会生成 minOccurs="0"。我已经修改了你的演示类来演示这个问题:
package moxy.test;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlType;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.persistence.Version;
public class Test {
@XmlType(propOrder = {})
public static class Root {
private Integer duration;
private Integer period;
public Integer getPeriod() {
return this.period;
}
public void setPeriod(Integer period) {
this.period = period;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
}
public static class MySchemaOutputResolver extends SchemaOutputResolver {
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(suggestedFileName);
return result;
}
}
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
System.out.println(jc);
System.out.println(Version.getVersionString());
jc.generateSchema(new MySchemaOutputResolver());
}
}
以下是用 MOXy 制作的。注意没有 minOccurs 属性:
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="root">
<xsd:all>
<xsd:element name="duration" type="xsd:int"/>
<xsd:element name="period" type="xsd:int"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
如果您删除 @XmlType(propOrder = {}) 注释,则生成的模式将存在 minOccurs 但元素作为序列:
org.eclipse.persistence.jaxb.JAXBContext@cdedfd
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="root">
<xsd:sequence>
<xsd:element name="duration" type="xsd:int" minOccurs="0"/>
<xsd:element name="period" type="xsd:int" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
仅供参考,以下是 Sun 的 JAXB 参考实现生成的内容:
jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl.class Build-Id: 1.6.0_21
...
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="root">
<xs:all>
<xs:element name="duration" type="xs:int" minOccurs="0"/>
<xs:element name="period" type="xs:int" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:schema>
提前致谢!