Search code examples
xmlxsdxsd-validationxml-validation

XSD: Cannot resolve the name to a(n) 'type definition' component


I am trying to validate XML using XSD. This is my XML file:

<PRODUCTS>
    <SELLER>ME</SELLER>
    <CURRENCY>EURO</CURRENCY>
  <PRODUCT>
    <NAME>LAPTOP</NAME>
    <MODEL>DELL</MODEL>
    <PRICE>2500</PRICE>
    <STOCK>100</STOCK>
  </PRODUCT>
  <PRODUCT>
    <NAME>PHONE</NAME>
    <MODEL>IPHONE</MODEL>
    <PRICE>1500</PRICE>
    <STOCK>100</STOCK>
  </PRODUCT>
  <PRODUCT>
    <NAME>TABLET</NAME>
    <MODEL>SAMSUNG</MODEL>
    <PRICE>3000</PRICE>
    <STOCK>700</STOCK>
  </PRODUCT>
</PRODUCTS>

and XSD file:

<xs:element name="PRODUCTS" type="ProductType"/>
  
  <xs:ComplexType name="ProductType">
    <xs:all>
        <xs:element type="xs:string" name="SELLER"/>
        <xs:element type="xs:string" name="CURRENCY"/>
        <xs:element type="Products" name="PRODUCT"/>
    </xs:all>
  </xs:ComplexType>
  
  <xs:ComplexType name="Products">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="PRODUCT" type="Products"/>
    </xs:sequence>
  </xs:ComplexType>
  
  <xs:complexType name="Products">
    <xs:sequence>
      <xs:element type="xs:string" name="NAMES"/>
      <xs:element type="xs:string" name="MODEL"/>
      <xs:element type="xs:int" name="PRICE"/>
      <xs:element type="xs:int" name="STOCK"/>
    </xs:sequence>
  </xs:complexType>
  
</xs:schema>

But when I am trying to validate this I get error

src-resolve: Cannot resolve the name 'ProductType' to a(n) 'type definition' component.

I can't figure it out why I am getting this error and how to fix this. Problem as I see is in the first line of element in XSD file.


Solution

  • XML and XSD are case-sensitive, so change all occurrences of xs:ComplexType in your XSD to xs:complexType to fix the immediate problem.


    Update, next issue migrated from comments:

    Now I get new error due to duplication of Products: A schema cannot contain two global components with the same name; this schema contains two occurrences of Products.

    A type cannot be defined with the same name in two different ways. It looks like the second Products should be Product in both the reference (@type) and the definition (xs:complexType/@name).