Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the purpose of minOccurs, nillable and restriction?
    text
    copied!<p>Documentation for <strong><a href="http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElement.html#required()" rel="noreferrer">required</a></strong> says:</p> <blockquote> <p>If <code>required()</code> is <code>true</code>, then Javabean property is mapped to an XML schema element declaration with <code>minOccurs="1"</code>. <code>maxOccurs</code> is <code>"1"</code> for a single valued property and <code>"unbounded"</code> for a multivalued property. </p> <p>If <code>required()</code> is <code>false</code>, then the Javabean property is mapped to XML Schema element declaration with <code>minOccurs="0"</code>. <code>maxOccurs</code> is <code>"1"</code> for a single valued property and <code>"unbounded"</code> for a multivalued property.</p> </blockquote> <p>Documentation for <strong><a href="http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElement.html#nillable()" rel="noreferrer">nillable</a></strong> says:</p> <blockquote> <p>If <code>nillable()</code> is <code>true</code>, then the JavaBean property is mapped to a XML Schema <code>nillable</code> element declaration.</p> </blockquote> <p><hr/> Code for <code>xs:complexType</code>:</p> <pre><code>public class WSData { //... @XmlElement(required = true, nillable = false) public void setMonth(XmlMonthType month) { this.month = month; } public void setUserLogin(String userLogin) { this.userLogin = userLogin; } } </code></pre> <p>Code for <code>xs:simpleType</code>:</p> <pre><code>@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; } </code></pre> <hr/> <p>Generated XML Schema:</p> <pre><code>&lt;xs:complexType name="wsData"&gt; &lt;xs:sequence&gt; &lt;xs:element name="month" type="xs:string"/&gt; &lt;xs:element minOccurs="0" name="userLogin" type="xs:string"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:simpleType name="xmlMonthType"&gt; &lt;xs:restriction base="xs:int"&gt; &lt;xs:enumeration value="1"/&gt; &lt;xs:enumeration value="2"/&gt; &lt;xs:enumeration value="3"/&gt; &lt;!-- ... months 4 ~9 ... --&gt; &lt;xs:enumeration value="10"/&gt; &lt;xs:enumeration value="11"/&gt; &lt;xs:enumeration value="12"/&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; </code></pre> <p><hr/> The facts:</p> <ol> <li>The default value for <strong>minOccurs</strong> is 1. So, <em>month</em> is required (must exist);</li> <li>The month has a <strong>restriction</strong>. So, <em>month</em> can only have a value defined by one of the 12 defined enumerations;</li> <li>The default value for <strong>nillable</strong> is false. So, <em>month</em> can't have empty values;</li> <li>The XML Schema is generated correctly.</li> </ol> <p>The problems:</p> <ol> <li>It is accepting month's abcense (mustn't exist);</li> <li>It is accepting any values for month, like 13 (except when isn't parseable to Integer);</li> <li>It is accepting empty values;</li> </ol> <p>I wasn't expecting these problems, am I missing something?<br/> If that behavior is correct, what is the purpose of <strong><a href="http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElement.html#required()" rel="noreferrer">required</a></strong>, <strong><a href="http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElement.html#nillable()" rel="noreferrer">nillable</a></strong> and <strong><a href="http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlEnumValue.html" rel="noreferrer">xs:restriction</a></strong>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload