36
votes

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?

1

1 Answers

57
votes

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.