Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

xml - nillable and minOccurs XSD element attributes

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...