xml validity with xsd with xsi:nillable element

4.2k Views Asked by At

My XML file

    <tns:SampleInfoResponse xsi:schemaLocation="sampleNS test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="sampleNS">
 <tns:SampleInfo>
  <tns:firstName>String</tns:firstName>
  <tns:lastName>String</tns:lastName>
  <tns:lbn>String</tns:lbn>
  <tns:LSampleDetails>
   <tns:SampleState>String</tns:SampleState>
   <tns:SampleId>String</tns:SampleId>
   <tns:sample>String</tns:sample>
   <tns:status>String</tns:status>
   <tns:statusDate>String</tns:statusDate>
   <tns:SampleId>String</tns:SampleId>
  </tns:LSampleDetails>
  **<tns:LSampleEnrlDetails/>**
  <tns:middleName>String</tns:middleName>
  <tns:element1>String</tns:element1>
  <tns:element2>String</tns:element2>
 </tns:SampleInfo>
</tns:SampleInfoResponse>

MY XSD file

    <xsd:schema targetNamespace="sampleNS" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="sampleNS" xmlns="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="SampleInfoResponse" type="tns:SampleInfoResponseV1"/>
 <xsd:complexType name="SampleInfoResponseV1">
  <xsd:sequence>
   <xsd:element name="SampleInfo" type="tns:SampleInfVO" minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="SampleInfVO">
  <xsd:sequence>
   <xsd:element name="firstName" type="xsd:string" nillable="true"/>
   <xsd:element name="lastName" type="xsd:string" nillable="true"/>
   <xsd:element name="lbn" type="xsd:string" nillable="true"/>
   <xsd:element name="LSampleDetails" type="tns:SampleDetailsVO" nillable="true" maxOccurs="unbounded"/>
   <xsd:element name="LSampleEnrlDetails" type="tns:SampleEnrlDetailsVO" nillable="true" maxOccurs="unbounded"/>
   <xsd:element name="middleName" type="xsd:string" nillable="true"/>
   <xsd:element name="element1" type="xsd:string" nillable="true"/>
   <xsd:element name="element2" type="xsd:string" nillable="true"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="SampleDetailsVO">
  <xsd:sequence>
   <xsd:element name="SampleState" type="xsd:string" nillable="true"/>
   <xsd:element name="SampleId" type="xsd:string" nillable="true"/>
   <xsd:element name="sample" type="xsd:string" nillable="true"/>
   <xsd:element name="status" type="xsd:string" nillable="true"/>
   <xsd:element name="statusDate" type="xsd:string" nillable="true"/>
   <xsd:element name="SampleId" type="xsd:string" nillable="true"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="SampleEnrlDetailsVO">
  <xsd:sequence>
   <xsd:element name="element1" type="xsd:string" nillable="true"/>
   <xsd:element name="element2" type="xsd:string" nillable="true"/>
   <xsd:element name="element3" type="xsd:boolean"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

parser is complaining on <tns:LSampleEnrlDetails/>, the XML file should be <tns:LSampleEnrlDetails xsi:nil="true"/> only for valid file ? By taking out the whole tag also the parser is complaining.

I would like to know what possible cases for this tag <tns:LSampleEnrlDetails/> makes the XML file valid according the above schema when i don't have the data to populate for tag <tns:LSampleEnrlDetails/>

2

There are 2 best solutions below

0
On

Use below attribute to make it valid

<tns:LSampleEnrlDetails xsi:nill="true" />
0
On

The xsi:nil attribute is needed because there's no way to distinguish between an element whose content is an empty string and one that has no content (ie, is null). So you use an attribute, which is "out of band" with respect to text content.

Personally, I don't like it, and think there are better alternatives. The simplest is to make your elements optional, and omit them if they contain null content.