我正在尝试验证xml文档是否使用Java遵守xsd规范。

这是我的Java代码:

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());
    }


这是我遇到的错误

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.


这是我的xsd文件:

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




问题出现在XXLocalDeliveryAsnPost的第二个complexType的开头(第14行)

我在这里做错了什么? xsd文档是由其他人提供的,无法更改。

最佳答案

您发布的XSD在xs:complexType元素下具有不少于五个匿名XXLocalDeliveryAsnPost。解析器在遇到第二个解析器后会举起手来,但是在此之后解析器会出现更多问题。

如果我不得不猜,似乎有人在快速复制,粘贴和祈祷最好的东西。

首先,假设第一个xs:complexType的结尾是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>


从其余的xs:complexTypes中,您可能希望将其他子元素拉到上面的注释中为其标记的位置。或者,您可能需要将这些其他xs:complexTypes与它们自己的元素或类型定义相关联。肯定要弄得一团糟,但这有望使您开始进行维修。

10-05 18:45