问题描述:

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.

网友答案:

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>
相关阅读:
Top