4

I'm trying to validate XSD aginst XML but getting an error

The element 'Table' has incomplete content. List of possible elements expected: 'IP21TAG'.

XML:

<NewDataSet> 
 <Table>
  <SITE>VMD</SITE>
  <TANK>65-12-392</TANK>
  <SERVICE>HZLPG</SERVICE>
 </Table>
 <Table>
  <SITE>VMD</SITE>
  <TANK>65-12-392</TANK>
  <SERVICE>HZLPG</SERVICE>
  <IP21TAG>BC-BBH-OS-4LI21392</IP21TAG>
 </Table>
</NewDataSet>

XSD:

 <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="NewDataSet">
 <xs:complexType>
 <xs:sequence>
 <xs:element name="Table">
 <xs:complexType>
 <xs:sequence>
 <xs:element name="SITE" type="xs:string" />
 <xs:element name="PLANT" type="xs:string" />
 <xs:element name="TANK" type="xs:string" />
 <xs:element name="SERVICE" type="xs:string" />
 <xs:element name="IP21TAG" type="xs:string" />
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 </xs:sequence>
  </xs:complexType>
  </xs:element>
 </xs:schema>

Can anyone help me how to solve this?

Thanks in advance.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
user2148679
  • 459
  • 1
  • 6
  • 18

2 Answers2

2

Obviously, solution is to set minOccurs="0" on elements that are optional.

However, error message in Visual Studio displayes even optional elements in the "possible element name" list, so it is not obvious if you miss minOccurs on an element.

My problem was that ONE element missed the minOccurs="0", and error message listed ALL, approximately 100, elements...

JERKER
  • 907
  • 8
  • 17
0

You omit minOccurs in <xs:element> node of your schema, its default is one (see specifications) then if you don't specify that node your XML won't be validated against that schema.

If that node is optional simply change your XSD to reflect that. Here I changed just IP21TAG and PLANT (because they're both not present in your example XML but if others are optional too you should change them accordingly):

<xs:element name="IP21TAG" type="xs:string" minOccurs="0"/>
<xs:element name="PLANT" type="xs:string" minOccurs="0" />

If that element is not optional then to be wrong is your XML, you may - for example - provide an empty string instead of a missing node:

<NewDataSet> 
 <Table>
  <SITE>VMD</SITE>
  <TANK>65-12-392</TANK>
  <SERVICE>HZLPG</SERVICE>
  <IP21TAG></IP21TAG>
  <PLANT></PLANT>
 </Table>
</NewDataSet>
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208