minOccurs,nillable和限制的目的是什么? [英] What's the purpose of minOccurs, nillable and restriction?
问题描述
必填 说:
Documentation for required says:
如果
required()
是
true
,然后Javabean属性映射到XML架构元素
声明,带有minOccurs =1
。对于单值
属性,maxOccurs
1
,unbounded
表示多值属性。
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.
如果 required()
是 false
,那么Javabean属性映射到XML
带有 minOccurs =0
的模式元素声明。对于
单值属性, maxOccurs
1
,unbounded
表示多值属性。
If required()
is false
, then the Javabean property is mapped to XML
Schema element declaration with minOccurs="0"
. maxOccurs
is "1"
for a
single valued property and "unbounded"
for a multivalued property.
nillable 说:
Documentation for nillable says:
如果
nillable()
是true
,那么JavaBean属性映射到XML
架构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架构:
Generated XML Schema:
<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>
事实:
The facts:
- minOccurs 的默认值为1.因此, month 是必需的(必须存在);
- 月份有限制。因此,月只能有12个定义的枚举之一定义的值;
- nillable 的默认值为false。因此,月不能包含空值;
- 正确生成XML架构。
- The default value for minOccurs is 1. So, month is required (must exist);
- The month has a restriction. So, month can only have a value defined by one of the 12 defined enumerations;
- The default value for nillable is false. So, month can't have empty values;
- The XML Schema is generated correctly.
问题:
- 接受月份的假设(必须不存在);
- 它接受月份的任何值,如13(除非不能解析为Integer);
- 接受空值;
我没想到会遇到这些问题,我错过了什么吗?
b $ b如果这种行为是正确的,那么<的目的是什么? strong> 必填 , nillable 和 xs:限制 ?
I wasn't expecting these problems, am I missing something?
If that behavior is correct, what is the purpose of required, nillable and xs:restriction?
推荐答案
Nillable允许空值。例如,如果您有一个Integer或Date,如果它是可为空的,则XML标记可能为空。如果它不是可存档但不是必需的,那么XML元素要么必须存在有效内容,要么根本不存在;空标签无效。
Nillable allows empty values. For example, if you have an Integer or a Date, if it's nillable, the XML tag could be empty. If it's not nillable but not required, the XML element would either have to exist with a valid content, or not exist at all; an empty tag wouldn't be valid.
这篇关于minOccurs,nillable和限制的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!