I have the below example XML that I want to validate against the below schema. I get an error regarding extraImages. The error is:
Error 3033: Element ‘{http://www.w3.org/2001/XMLSchema}sequence’: The
content is not valid. Expected is (annotation?, (element | group |
choice | sequence | any)*).
Can anyone see what I am doing wrong? When I have the extraImages complex type in a separate schema, it works, but does not work when I add it to the products schema.
<products>
<product>
<productImageURL>imgp9241.jpg</productImageURL>
<productDescription>blah blah blah</productDescription>
<productName>Test Extra</productName>
<extraImages>
<extraImages>
<fileName>textextra.jpg</fileName>
<sequence>10</sequence>
</extraImages>
<extraImages>
<fileName>textextra.jpg</fileName>
<sequence>10</sequence>
</extraImages>
</extraImages>
</product>
</products>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="products">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="product" minOccurs = "1" maxOccurs="1" type="productType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="productType">
<xsd:sequence>
<xsd:element name="productImageURL">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="0" />
<xsd:maxLength value="450" />
<xsd:whiteSpace value="collapse" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="productDescription">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:whiteSpace value="collapse"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="productName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="150"/>
<xsd:whiteSpace value="collapse"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="extraImages">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="extraImage" minOccurs = "1" maxOccurs="100" type="imgType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="imgType">
<xsd:sequence>
<xsd:element name="fileName">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="25"/>
<xsd:whiteSpace value="collapse"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="sequence" type="xsd:integer" default="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The error message tells you that the XSD is invalid. You can’t have a named type nested inside anything else other than a schema or redefine.
The corrected XSD:
It shows this layout:
After fixing the XSD, you can try to validate the XML you’ve shown. When looking at its structure while comparing it with the XSD…
You can see that the extraImages nested within the extraImages should be without an
s.