的文档 required 说:
If
required()
istrue
, then Javabean property is mapped to an XML schema element declaration withminOccurs="1"
.maxOccurs
is"1"
for a single valued property and"unbounded"
for a multivalued property.If
required()
isfalse
, then the Javabean property is mapped to XML Schema element declaration withminOccurs="0"
.maxOccurs
is"1"
for a single valued property and"unbounded"
for a multivalued property.
的文档 nillable 说:
If
nillable()
istrue
, then the JavaBean property is mapped to a XML Schemanillable
element declaration.
xs:complexType
的代码:
public class WSData {
//...
@XmlElement(required = true, nillable = false)
public void setMonth(XmlMonthType month) {
this.month = month;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
}
xs:simpleType
的代码:
@XmlType
@XmlEnum(Integer.class)
public enum XmlMonthType {
@XmlEnumValue("1")
JANUARY,
@XmlEnumValue("2")
FEBRUARY,
@XmlEnumValue("3")
MARCH,
/* ... months 4 ~9 ... */
@XmlEnumValue("10")
OCTOBER,
@XmlEnumValue("11")
NOVEMBER,
@XmlEnumValue("12")
DECEMBER;
}
生成的 XML 架构:
<xs:complexType name="wsData">
<xs:sequence>
<xs:element name="month" type="xs:string"/>
<xs:element minOccurs="0" name="userLogin" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="xmlMonthType">
<xs:restriction base="xs:int">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
<!-- ... months 4 ~9 ... -->
<xs:enumeration value="10"/>
<xs:enumeration value="11"/>
<xs:enumeration value="12"/>
</xs:restriction>
</xs:simpleType>
事实:
- minOccurs 的默认值为 1。因此,month 是必需的(必须存在);
- 月份有限制。因此,month 只能具有由 12 个已定义枚举之一定义的值;
- nillable 的默认值为 false。所以,month 不能有空值;
- XML 架构已正确生成。
问题:
- 它正在接受一个月的缺席(不能存在);
- 它接受月份的任何值,例如 13(除非无法解析为整数);
- 接受空值;
我没想到会出现这些问题,我是不是漏掉了什么?
如果该行为是正确的, required 的目的是什么? , nillable 和 xs:restriction ?
最佳答案
Nillable 允许空值。例如,如果您有一个整数或一个日期,如果它可以为空,则 XML 标记可以为空。如果它不是可空的但不是必需的,则 XML 元素要么必须存在且内容有效,要么根本不存在;空标签无效。
关于java - minOccurs、nillable 和 restriction 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9111936/