Search code examples
xmlxsdxml-validationxmlschemaset

cvc-complex-type.2.4.b: The content of element 'tns:patient' is not complete


In the below xsd I am trying to create a list of 10 patients using the same schema. When I validate the xml file I get an error saying cvc complex type 2.4.b.Is there any other way I can use to create a list of 10 patients with the same schema definition.

XSD file:

<?xml version="1.0" encoding="UTF-8"?>
 <schema xmlns="http://www.w3.org/2001/XMLSchema"  targetNamespace="http://www.hennaloungani.com/Patient"
xmlns:tns="http://www.hennaloungani.com/Patient"  elementFormDefault="qualified">

 <element name="patient" type="tns:Patient" />
<complexType name="Patient">
    <sequence>
        <element name="patient" type="tns:Patient" maxOccurs="10" />
        <element name="name" type="tns:Sting15Char"></element>
        <element name="age" type="int"></element>
        <element name="dob" type="date"></element>
        <element name="email" type="string" maxOccurs="unbounded"></element>
        <element name="gender" type="tns:Gender"></element>
        <element name="phone" type="string"></element>
        <element name="payment" type="tns:PaymentType"></element>
    </sequence>

    <attribute name="id" type="tns:ID"></attribute>
    </complexType>


    <complexType name="PaymentType">
        <choice>
            <element name="Cash" type="int"></element>
            <element name="Insurance" type="tns:Insurance"></element>
        </choice>
    </complexType>
    <complexType name="Insurance">
        <all>
            <element name="provider" type="string"></element>
            <element name="limit" type="int"></element>

        </all>

    </complexType>
    <simpleType name="ID">
        <restriction base="int">
            <pattern value="[0-100]"></pattern>
        </restriction>
    </simpleType>

    <simpleType name="Sting15Char">
        <restriction base="string">
            <maxLength value="15"></maxLength>
        </restriction>
    </simpleType>
    <simpleType name="Gender">
        <restriction base="string">
            <enumeration value="M"></enumeration>
            <enumeration value="F"></enumeration>
        </restriction>
    </simpleType>

I am getting this error at line 2 : cvc-complex-type.2.4.b: The content of element 'tns:patient' is not complete. One of '{"http:// www.hennaloungani.com/Patient":patient}' is expected

Xml file:

<?xml version="1.0" encoding="UTF-8"?>
<tns:patient id="1" xmlns:tns="http://www.hennaloungani.com/Patient"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hennaloungani.com/Patient Patient.xsd ">
<tns:patient id="1" />
<tns:name>tns:name</tns:name>
<tns:age>0</tns:age>
<tns:dob>2001-01-01</tns:dob>
<tns:email>tns:email</tns:email>
<tns:gender>M</tns:gender>
<tns:phone>tns:phone</tns:phone>
<tns:payment>
    <tns:Cash>0</tns:Cash>
</tns:payment>


Solution

  • The patient child of the patient root element of the XML has no children of its own, yet the XSD says that patient elements must have a sequence of various child elements (patient, name, age, etc).

    If you want to have any hope of instantiating a valid XML document for your XSD, you'll at least have to make the patient child element be optional: minOccurs="0". Otherwise, your XSD will be specifying an infinite recursion of patient descendants. Note that by default, minOccurs="1", which means that the element is required.

    If you want to allow your XML as-is (with an empty patient child element) to validate, similarly add minOccurs="0" to each of the declarations for name, age, dob, etc.