MinOccurs 0和nillable true

时间:2016-04-20 01:27:56

标签: xml soap xsd wsdl

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

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

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

<birthDate/>

2 个答案:

答案 0 :(得分:21)

设置nillable="true"表示<birthDate>标记可以显示如下:

<birthDate xsi:nil="true"/>

但是,由于您还设置了minOccurs="0",因此您还可以完全从XML中省略<birthDate>标记,它仍然可以针对您的XSD进行验证。

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

查看this great blog帖子以便进一步阅读。

答案 1 :(得分:0)

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

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

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.

  • 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 xsi for "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.