MinOccurs 0 and nillable true

58,952

Solution 1

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

<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.

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

Have a look at this great blog post for further reading.

Solution 2

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.

Share:
58,952
SpongebobJunior
Author by

SpongebobJunior

Something to work?

Updated on February 04, 2020

Comments

  • SpongebobJunior
    SpongebobJunior over 4 years

    In my wsdl I have an element:

    <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

    <birthDate/>
    
  • Michael Kay
    Michael Kay about 8 years
    Setting nillable="true" does not allow you to write <birthDate/>, it only allows you to write <birthDate xsi:nil="true"/>. Which probably explains why no-one actually uses the feature.
  • Tim Biegeleisen
    Tim Biegeleisen about 8 years
    @MichaelKay Thank you for correcting me and I apologize for the initial misinformation.