I am going back and forth with setting an element to minOccurs="0"
and nillable="true"
.
I was reading this article and now in my WSDL I'm not sure if using both is worth it. The article gives a good example of representing arrays where you might have null values interspersed throughout, as this can't be done with just minOccurs="0"
. Now, the convention I've been going with is that if an element isn't optional it is not nillable.
The difference as I understand it, and where my question lies, is that by applying the nillable property to an element, I am saying that you can pass in the XSD equivalent of a NULL value? Otherwise, an element without the nillable property, has to have a value within the restriction placed on it?
The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.
The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.
nillable="false" means you can't have the attribute xsi:nil="true". But you don't have this attribute so this won't make it invalid.
The default is false. If nillable is true, this enables an instance of the element to have the nil attribute set to true. The nil attribute is defined as part of the XML Schema namespace for instances.
You need to decide whether you are thinking about XML as XML, or whether you are thinking about XML as a way to transmit Java (or other) object from here to there.
In XML, nillable permits the construction <myelement xsi:nil='true'/>
as an indicator of an explicit absent value, like an SQL NULL. This is semantically different from just <myelement/>
. And both are distinct from nothing at all. So, when looking at XML, you have to distinguish four cases:
<!-- nothing -->
<myElement attr1='true'>some content</myElement>
<myElement/>
<myElement xsi:nil='true'/>
If, on the other hand, you are primarily concerned with Java -- perhaps because you are using SOAP, then you need to think about how Java object map back and forth.
For any Java item that inherits from Object, JAXB and other mapping technologies need a way to deal with null values. Nillable is the way to do that. If you forbid nillable on something that can be an object, toolkits will annoyingly use an array to find a way to represent absence.
On the other hand, if you have an array, keep in mind that the array itself is an object, and can be null. So, every toolkit has to distinguish a zero-element array from a null.
On the other hand, if you have a primitive type (e.g. int
), nillable will lead to problems, since there is no mapping from xsi:nil to a primitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With