minOccurs,nillable和限制的目的是什么? [英] What's the purpose of minOccurs, nillable and restriction?

查看:260
本文介绍了minOccurs,nillable和限制的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必填 说:

Documentation for required says:


如果 required()
true ,然后Javabean属性映射到XML架构元素
声明,带有 minOccurs =1。对于单值
属性, maxOccurs 1unbounded 表示多值属性。

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.

如果 required() false ,那么Javabean属性映射到XML
带有 minOccurs =0的模式元素声明。对于
单值属性, maxOccurs 1unbounded 表示多值属性。

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() 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架构:


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:


  1. minOccurs 的默认值为1.因此, month 是必需的(必须存在);

  2. 月份有限制。因此,只能有12个定义的枚举之一定义的值;

  3. nillable 的默认值为false。因此,不能包含空值;

  4. 正确生成XML架构。

  1. The default value for minOccurs is 1. So, month is required (must exist);
  2. The month has a restriction. So, month can only have a value defined by one of the 12 defined enumerations;
  3. The default value for nillable is false. So, month can't have empty values;
  4. The XML Schema is generated correctly.

问题:


  1. 接受月份的假设(必须不存在);

  2. 它接受月份的任何值,如13(除非不能解析为Integer);

  3. 接受空值;

我没想到会遇到这些问题,我错过了什么吗?
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆