首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSD文档解析错误

XSD文档解析错误
EN

Stack Overflow用户
提问于 2014-12-08 18:54:56
回答 2查看 252关注 0票数 0

我试图使用Java验证xml文档是否符合xsd规范。

以下是我的java代码:

代码语言:javascript
复制
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());
    }  

这是我正在犯的错误

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<?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文档是由其他人提供的,不能更改。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-09 04:46:59

您发布的XSD在XXLocalDeliveryAsnPost元素下至少有五个匿名的XXLocalDeliveryAsnPost。解析器在遇到第二个解析器后,会举起手来,但是在那之后,它会遇到更多的问题。

如果我不得不猜的话,看上去像是有人做了一些快速的复制、粘贴和祈祷。

为了让您开始,让我们通过假设第一个xs:complexType的末尾是XXLocalDeliveryAsnPost内容模型定义的正确结尾来修复第一个问题

代码语言:javascript
复制
<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与它们自己的元素或类型定义关联起来。这是一个太多的混乱,无法确定,但希望这将使你开始修理。

票数 1
EN

Stack Overflow用户

发布于 2014-12-08 19:01:27

<complexType>元素中有两个<element>元素--只允许一个。事情就是这么简单。

如果您提供了XSD应该描述的XML,我们可能能够修补XSD。

下面只是一个经过深思熟虑的猜测,但是无论“其他人”说什么,XML都必须修改。(你可能会礼貌地指出,在分发这些文件之前,可以简单、廉价地检查这些文件。)

代码语言:javascript
复制
<?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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27364901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档