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

标签 java xml jaxb wsdl xsd

的文档 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/

相关文章:

xml - 如何使用 xmlstarlet 选择多个同名元素?

java - 无法解析 REST Web 服务中的 JSON

java - 如何使用 JAXB 编码有时包含 XML 内容有时不包含的字符串?

java - 处理数组中的对象时出现越界

java - 拦截器刚刚在欢迎页面之前调用

sql-server - SQL Server 'FOR XML' 获取值到 "row"元素节点

java - 使用 JAXB/JAX-RS 将 XML 元素包装在子元素中

java - 如何在 JAXB 中映射已有的 java bean

java - 如何使用 Java/Processing 调用子类上的方法?

java - 无法从 oracle 数据库读取德语字符