gpt4 book ai didi

java - minOccurs、nillable 和 restriction 的目的是什么?

转载 作者:太空狗 更新时间:2023-10-29 22:54:21 25 4
gpt4 key购买 nike

的文档 required 说:

If required() is true, then Javabean property is mapped to an XML schema element declaration with minOccurs="1". maxOccurs is "1" for a single valued property and "unbounded" for a multivalued property.

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 说:

If nillable() is true, then the JavaBean property is mapped to a XML Schema nillable 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>


事实:

  1. minOccurs 的默认值为 1。因此,month 是必需的(必须存在);
  2. 月份有限制。因此,month 只能具有由 12 个已定义枚举之一定义的值;
  3. nillable 的默认值为 false。所以,month 不能有空值;
  4. XML 架构已正确生成。

问题:

  1. 它正在接受一个月的缺席(不能存在);
  2. 它接受月份的任何值,例如 13(除非无法解析为整数);
  3. 接受空值;

我没想到会出现这些问题,我是不是漏掉了什么?
如果该行为是正确的, required 的目的是什么? , nillable xs:restriction ?

最佳答案

Nillable 允许空值。例如,如果您有一个整数或一个日期,如果它可以为空,则 XML 标记可以为空。如果它不是可空的但不是必需的,则 XML 元素要么必须存在且内容有效,要么根本不存在;空标签无效。

关于java - minOccurs、nillable 和 restriction 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9111936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com