Cannot generate classes because no top-level elements with complex type where found

Code_Viking picture Code_Viking · Sep 4, 2015 · Viewed 10.2k times · Source

I am trying to create a class from a xsd file using the XSD.EXE tool. but for some reason i get this error.

Warning: cannot generate classes because no top-level elements with complex type were found.

I have looked around on stack and seen that i could put a type on the complex type element but i cannot seem to get i to work. here is the xsd file

    <?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     targetNamespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
     xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
     elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:complexType name="DocumentIdentification">
    <xs:sequence>
      <xs:element name="Standard" type="xs:string"/>
      <xs:element name="TypeVersion" type="xs:string"/>
      <xs:element name="InstanceIdentifier" type="xs:string"/>
      <xs:element name="Type" type="xs:string"/>
      <xs:element name="MultipleType" type="xs:boolean" minOccurs="0"/>
      <xs:element name="CreationDateAndTime" type="xs:dateTime"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Thanks!

Answer

Abel picture Abel · Sep 12, 2015

You XSD only defines a type (as Sergio also suggested). As such, it cannot be used for validation of XML, unless it is imported by another XSD . Likewise, other tools like xsd.exe will not be able to anything sensible with it.

You can compare this with a C# library having an interface definition, but no implementation of the interface.

You can fix this in a variety of ways. Considering your current code, I would suggest something along those lines:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
    xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:complexType name="DocumentIdentification">
        <xs:sequence>
            <xs:element name="Standard" type="xs:string" />
            <xs:element name="TypeVersion" type="xs:string" />
            <xs:element name="InstanceIdentifier" type="xs:string" />
            <xs:element name="Type" type="xs:string" />
            <xs:element name="MultipleType" type="xs:boolean" minOccurs="0" />
            <xs:element name="CreationDateAndTime" type="xs:dateTime" />
        </xs:sequence>
    </xs:complexType>

    <xs:element name="DocumentIdentification" type="DocumentIdentification" />
</xs:schema>

Though you may consider renaming the type name, to prevent confusion to readers. A common pattern is to suffix typenames with Type, in your case, DocumentIdentificationType.

The code above is importable with xsd.exe without any problems.