nillable 和 minOccurs XSD 元素属性 [英] nillable and minOccurs XSD element attributes

查看:22
本文介绍了nillable 和 minOccurs XSD 元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个元素设置为 minOccurs="0"nillable="true".

I am going back and forth with setting an element to minOccurs="0" and nillable="true".

我正在阅读这篇文章,现在在我的 WSDL 我不确定使用两者是否值得.这篇文章给出了一个很好的例子来表示数组,其中你可能会在整个数组中散布空值,因为这不能仅使用 minOccurs="0" 来完成.现在,我一直采用的约定是,如果元素不是可选的,则它不可为空.据我所知,不同之处在于我的问题所在,是通过将 nillable 属性应用于元素,我是说您可以传入与 NULL 值等效的 XSD 值?否则,一个没有 nillable 属性的元素,必须在限制范围内有一个值吗?

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?

推荐答案

您需要决定是将 XML 视为 XML,还是将 XML 视为从此处传输 Java(或其他)对象的一种方式到那里.

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.

在 XML 中,nillable 允许构造 <myelement xsi:nil='true'/> 作为显式缺失值(如 SQL NULL)的指示符.这在语义上与 不同.两者都完全不同.因此,在查看 XML 时,您必须区分四种情况:

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'/>

另一方面,如果您主要关注 Java —— 可能是因为您使用的是 SOAP,那么您需要考虑 Java 对象如何来回映射.

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.

对于任何继承自 Object 的 Java 项,JAXB 和其他映射技术都需要一种处理空值的方法.Nillable 就是这样做的方法.如果您禁止对可以是对象的事物进行 nillable,那么工具包会烦人地使用数组来寻找表示不存在的方法.

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.

另一方面,如果你有一个原始类型(例如 int),nillable 会导致问题,因为没有从 xsi:nil 到原始类型的映射.

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.

这篇关于nillable 和 minOccurs XSD 元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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