CuriousCoder
CuriousCoder

Reputation: 1600

Error parsing XSD document

I am trying to validate that an xml document adheres to an xsd specification using Java.

Here is my java code:

SchemaFactory factory = 
        SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

File schemaLocation = new File("XX_ASN.xsd");
    Schema schema = factory.newSchema(schemaLocation); //**Error occurs here**

Validator validator = schema.newValidator();


    String xmlFile="file://18828324.txt";
    Source source = new StreamSource(xmlFile);

    // 5. Check the document
    try {
        validator.validate(source);
        System.out.println(args[0] + " is valid.");
    }
    catch (SAXException ex) {
        System.out.println(args[0] + " is not valid because ");
        System.out.println(ex.getMessage());
    }  

Here is the error i am getting

org.xml.sax.SAXParseException; systemId: file:/C:/workspace4.3/MMSV/XX_ASN.xsd; lineNumber: 14; columnNumber: 19; s4s-elt-must-match.1: The content of 'XXLocalDeliveryAsnPost' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: complexType.

And here is my xsd file:

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xs:element name="XXLocalDeliveryAsnPost">
    <xs:annotation>
        <xs:documentation>Post ASN data to XX Local Delivery</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="VendorId" type="xs:string">
                <!-- Name of Vendor provided by XX -->
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="unbounded">
            <xs:element name="AsnData">
                <xs:annotation>
                    <xs:documentation>ASN for each truck</xs:documentation>
                </xs:annotation>

                <xs:complexType>
                    <xs:element name="ShipOrigin" type="xs:string"/>
                    <xs:element name="Scac" type="xs:string">
                        <!-- Will store upto 4 characters -->
                    </xs:element>
                    <xs:element name="VehicleId" type="xs:string">
                        <!-- Will store upto 20 characters -->
                    </xs:element>
                    <xs:element name="BolNumber" type="xs:string">
                        <!-- Will store upto 8 characters -->
                    </xs:element>
                    <xs:element name="VendorShipDateToXX" type="xs:string">
                        <!-- YYYYMMDD Format -->
                    </xs:element>
                    <xs:element name="VendorArrivalDateAtXX" type="xs:string">
                        <!-- YYYYMMDD Format -->
                    </xs:element>
                    <xs:element name="VendorArrivalTimeAtXX" type="xs:string">
                        <!-- HHMM Miltary Format -->
                    </xs:element>
                    <xs:element name="OrderData">
                        <xs:annotation>
                            <xs:documentation>Order Data</xs:documentation>
                        </xs:annotation>

                        <xs:complexType>
                            <xs:sequence minOccurs="1" maxOccurs="unbounded">
                                <xs:element name="XXOrderNumber" type="xs:string"/>
                                <xs:element name="VendorShipDateToXX" type="xs:string">
                                    <!-- YYYYMMDD Format -->
                                </xs:element>
                                <xs:element name="ModelData">
                                    <xs:annotation>
                                        <xs:documentation>Model Data</xs:documentation>
                                    </xs:annotation>

                                <xs:complexType>
                                    <xs:sequence minOccurs="1" maxOccurs="unbounded">
                                        <xs:element name="ModelNumber" type="xs:string">
                                            <!-- Will store upto 20 characters -->
                                        </xs:element>
                                        <xs:element name="SerialNumber" type="xs:string">
                                            <!-- Will store upto 30 characters -->
                                        </xs:element>
                                        <xs:element name="WeightInPounds" type="xs:string">
                                            <!-- Format 999.999 Weight in Pounds (lbs) -->
                                        </xs:element>
                                        <xs:element name="LengthInInches" type="xs:string">
                                            <!-- Format 999.999 Length in Inches -->
                                        </xs:element>
                                        <xs:element name="WidtInInches" type="xs:string">
                                            <!-- Format 999.999 Width in Inches -->
                                        </xs:element>
                                        <xs:element name="HeightInInches" type="xs:string">
                                            <!-- Format 999.999 Height in Inches -->
                                        </xs:element>
                                        <xs:element name="ModelFreightCode" type="xs:string">
                                            <!-- Will store upto 6 characters -->
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

The problem occurs in the start of second complexType of XXLocalDeliveryAsnPost (line 14)

what am i doing wrong here? The xsd document was provided by someone else and cannot be changed.

Upvotes: 0

Views: 295

Answers (2)

kjhughes
kjhughes

Reputation: 111551

The XSD that you posted has no less than five anonymous xs:complexTypes under the XXLocalDeliveryAsnPost element. The parser is throwing its hands up after encountering the second one, but there're more problems coming down the pike for it after that.

If I had to guess, it looks like someone did some quick copying, pasting, and praying for the best.

To get you started, let's repair the first problem by assuming that the end of the first xs:complexType is the correct end of the definition of the content model for XXLocalDeliveryAsnPost:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="XXLocalDeliveryAsnPost">
    <xs:annotation>
      <xs:documentation>Post ASN data to XX Local Delivery</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="VendorId" type="xs:string">

        <!--  Add additional children elements here -->

        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

From the remaining xs:complexTypes, you may want to pull additional children elements to the place marked for them in the comment above. Or, you might need to associate those additional xs:complexTypes with their own elements or type definitions. It's too much of a mess to know for sure, but this will hopefully get you started making the repairs.

Upvotes: 1

laune
laune

Reputation: 31290

You have two <complexType> elements in the <element>element - only one is permitted. It's as simple as that.

If you provide the XML this XSD should describe, we might be able to patch the XSD.

Below is just an educated guess but the XML Schema will have to be changed no matter what "someone else" says. (You might point out, politely, that such files can be checked cheaply and easily before they are handed out.)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="XXLocalDeliveryAsnPost">
<xs:annotation>
    <xs:documentation>Post ASN data to XX Local Delivery</xs:documentation>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element name="VendorId" type="xs:string">
      <!-- Name of Vendor provided by XX -->
    </xs:element>
    <xs:element name="AsnData" minOccurs="1" maxOccurs="unbounded">
      <xs:annotation>
        <xs:documentation>ASN for each truck</xs:documentation>
      </xs:annotation>
      <xs:complexType>
        <xs:sequence>
          <xs:element name="ShipOrigin" type="xs:string"/>
          <xs:element name="Scac" type="xs:string">
                    <!-- Will store upto 4 characters -->
          </xs:element>
          <xs:element name="VehicleId" type="xs:string">
                    <!-- Will store upto 20 characters -->
          </xs:element>
          <xs:element name="BolNumber" type="xs:string">
                    <!-- Will store upto 8 characters -->
          </xs:element>
          <xs:element name="VendorShipDateToXX" type="xs:string">
                    <!-- YYYYMMDD Format -->
          </xs:element>
          <xs:element name="VendorArrivalDateAtXX" type="xs:string">
                    <!-- YYYYMMDD Format -->
          </xs:element>
          <xs:element name="VendorArrivalTimeAtXX" type="xs:string">
                 <!-- HHMM Miltary Format -->
          </xs:element>
          <xs:element name="OrderData">
             <xs:annotation>
                        <xs:documentation>Order Data</xs:documentation>
             </xs:annotation>
             <xs:complexType>
               <xs:sequence minOccurs="1" maxOccurs="unbounded">
                 <xs:element name="XXOrderNumber" type="xs:string"/>
                 <xs:element name="VendorShipDateToXX" type="xs:string">
                        <!-- YYYYMMDD Format -->
                 </xs:element>
                 <xs:element name="ModelData">
                   <xs:annotation>
                      <xs:documentation>Model Data</xs:documentation>
                   </xs:annotation>
                   <xs:complexType>
                     <xs:sequence minOccurs="1" maxOccurs="unbounded">
                       <xs:element name="ModelNumber" type="xs:string">
                         <!-- Will store upto 20 characters -->
                       </xs:element>
                       <xs:element name="SerialNumber" type="xs:string">
                         <!-- Will store upto 30 characters -->
                       </xs:element>
                       <xs:element name="WeightInPounds" type="xs:string">
                         <!-- Format 999.999 Weight in Pounds (lbs) -->
                       </xs:element>
                       <xs:element name="LengthInInches" type="xs:string">
                         <!-- Format 999.999 Length in Inches -->
                       </xs:element>
                       <xs:element name="WidtInInches" type="xs:string">
                           <!-- Format 999.999 Width in Inches -->
                       </xs:element>
                       <xs:element name="HeightInInches" type="xs:string">
                                        <!-- Format 999.999 Height in Inches -->
                       </xs:element>
                       <xs:element name="ModelFreightCode" type="xs:string">
                         <!-- Will store upto 6 characters -->
                       </xs:element>
                     </xs:sequence>
                   </xs:complexType>
                 </xs:element>
               </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Upvotes: 1

Related Questions