xml MinOccurs 0 和 nillable true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36732264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:11:29  来源:igfitidea点击:

MinOccurs 0 and nillable true

xmlsoapxsdwsdl

提问by SpongebobJunior

In my wsdl I have an element:

在我的 wsdl 中,我有一个元素:

<xsd:element minOccurs="0" name="birthDate" nillable="true" type="xsd:dateTime"/>

I know that the nillable true allows null values does this means that it can allow xml empty tag? i.e

我知道 nillable true 允许空值是否意味着它可以允许 xml 空标签?IE

<birthDate/>

回答by Tim Biegeleisen

Setting nillable="true"means that the <birthDate>tag can appear as follows:

设置nillable="true"意味着<birthDate>标签可以如下所示:

<birthDate xsi:nil="true"/>

However, since you also set minOccurs="0", you could also omit the <birthDate>tag completely from the XML and it would also still validate against your XSD.

但是,由于您还设置了minOccurs="0",您也可以<birthDate>完全从 XML 中省略标记,并且它仍然会根据您的 XSD 进行验证。

Note that <birthDate/>or <birthDate></birthDate>is not considered null according to XSD rules.

请注意,根据 XSD 规则,<birthDate/>or<birthDate></birthDate>不被视为空值。

Have a look at this great blogpost for further reading.

看看这篇很棒的博客文章以进一步阅读。

回答by krishna kanth

Adding my view to above answers, A basic thing that many beginners don't know or take into consideration is binding the xsivariable with Schema Instance namespace.

将我的观点添加到上述答案中,许多初学者不知道或不考虑的基本事情是将xsi变量与 Schema Instance 命名空间绑定。

Eg: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" [add this as a attribute anywhere in a xml opening tag].

例如: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" [将此作为属性添加到 xml 开始标记中的任何位置]。

The attribute prefix "xsi" in this case has to be bonded with the XML namespace "http://www.w3.org/2001/XMLSchema-instance". This binding can be done in any of the parent elements or in the root element itself, Where to do the binding depends on the scope for which you want the xsi to be available.

在这种情况下,属性前缀“ xsi”必须与 XML 命名空间“ http://www.w3.org/2001/XMLSchema-instance”绑定。这种绑定可以在任何父元素或根元素本身中完成,在哪里进行绑定取决于您希望 xsi 可用的范围。

  • All the elements in nested to to declaration gets the same value
  • Even though you can use any name for binding the namespcae, for brevity its always recommended to use xsifor "http://www.w3.org/2001/XMLSchema-instance"
  • 嵌套到 to 声明中的所有元素都获得相同的值
  • 即使您可以使用任何名称来绑定namespcae,但为简洁起见,始终建议将xsi用于http://www.w3.org/2001/XMLSchema-instance

PS : I realized the whole importance of xml namespace bindings and prefixing attributes wherever required, when I struggled at work by staying back for 3 extra hours to understand, why my xml node is not getting validated by its xsd even in case of a nillable attribute in present in schema definition.

PS:我意识到 xml 命名空间绑定和在任何需要的地方添加前缀属性的全部重要性,当我在工作中挣扎了 3 个额外小时来理解为什么我的 xml 节点没有得到它的 xsd 验证时,即使是一个可空属性在模式定义中。