1
votes

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>
2

2 Answers

4
votes

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:

<?xml version="1.0"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<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:sequence>
    </xsd:complexType>
    <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:schema>

It shows this layout:

enter image description here

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...

enter image description here

You can see that the extraImages nested within the extraImages should be without an s.

Error occurred while loading [], line 7 position 5
The element 'extraImages' has invalid child element 'extraImages'. List of possible elements expected: 'extraImage'.
0
votes

In the XSD, you are using two different elements: extraImages and extraImage
But in your XML, you are using extraImages twice instead of using extraImages and extraImage once each