Xml – The type attribute cannot be present with either simpleType or complexType

xml

I have the below XSD & when I am trying to generate XML out of it I am getting the above error : Error!!! The type attribute cannot be present with either simpleType or complexType. Need your help suggestions in resolving the issue.

`<?xml version="1.0" encoding="utf-8"?><xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="InvoiceData" type="InvoiceData">
<xs:complexType name="InvoiceData">
<xs:sequence>
<xs:element name="HeaderFields" type="HeaderFields">
<xs:complexType name="HeaderFields">
<xs:sequence>
<xs:element name="CompanyId" type="xs:string" />
<xs:element name="ImageID" type="xs:string" />
<xs:element name="Incident" type="xs:string" />
<xs:element name="FacilityID" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>`

Best Answer

If you define the type of your element inline you can't name it so remove the type="InvoiceData" attribute (and then the name="InvoiceData" attribute as well).

If you want to use those attributes then you need to separate element and type definition e.g. <xsl:element name="InvoiceData" type="InvoiceData"/> and <xsl:complexType name="InvoiceData">...</xsl:complexType>.

The complete schema would be either

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="InvoiceData">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="HeaderFields">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CompanyId" type="xs:string" />
              <xs:element name="ImageID" type="xs:string" />
              <xs:element name="Incident" type="xs:string" />
              <xs:element name="FacilityID" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

or

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="InvoiceData" type="InvoiceData"/>

  <xs:complexType name="InvoiceData">
    <xs:sequence>
      <xs:element name="HeaderFields" type="HeaderFields"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="HeaderFields">
    <xs:sequence>
      <xs:element name="CompanyId" type="xs:string" />
      <xs:element name="ImageID" type="xs:string" />
      <xs:element name="Incident" type="xs:string" />
      <xs:element name="FacilityID" type="xs:string" />
    </xs:sequence>
  </xs:complexType>

</xs:schema>
Related Topic