xml架构验证问题

时间:2012-04-07 04:55:34

标签: xml

我正在尝试使用xml架构验证器,但是我收到一条错误消息,说“文件过早结束”我似乎无法找到此错误的位置。 这是我的xsd文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="orders">
 <xs:element name ="order" maxOccurs="unbounded">
  <xs:complexType>
   <xs:sequence>  
    <xs:element name="case" type="xs:caseColor" >
      <xs:simpleType name="caseColor">
        <xs:restriction base="string">
            <xs:enumeration value="Lemonde" />
            <xs:enumeration value="Strawberry" />
            <xs:enumeration value="Lime" />
            <xs:enumeration value="Blueberry" />
        </xs:restriction> 
      </xs:simpleType>
    </xs:element>
    <xs:element name="batteries" type="xs:numOfBat" default = 
"1"> 
      <xs:simpleType name="numOfBat">
        <xs:restriction base="integer">
            <xs:enumeration value="1" />
            <xs:enumeration value="2" />
            <xs:enumeration value="3" />
            <xs:enumeration value="4" />
        </xs:restriction> 
      </xs:simpleType>
    </xs:element>
    <xs:element name="recharger" type="xs:volt"> 
    <xs:simpleType name="volt">
        <xs:restriction base="string">
            <xs:enumeration value="110-120" />
            <xs:enumeration value="220-240" />
        </xs:restriction> 
    </xs:simpleType>
    </xs:element>
    <xs:element name="arm"> 
       <xs:element name ="reaches" minOccurs="2" maxOccurs="3" 
type="xs:reach">
         <xs:simpleType name="reach">
            <xs:restriction base = "integer">
              <xs:enumeration value = "50" />
              <xs:enumeration value = "75" /> 
              <xs:enumeration value = "100" />
             </xs:restriction>
     </xs:simpleType>
    </xs:element>
   </xs:element>
    <xs:element name ="camera" type="numOfCam"> 
      <xs:simpleType name="numOfCam">
        <xs:restriction base="string">
            <xs:enumeration value="1" />
            <xs:enumeration value="2" />
        </xs:restriction> 
      </xs:simpleType>
    </xs:element>
    <xs:element name = "speech" type="xs:language">
      <xs:simpleType name = "language">
        <xs:restriction base = "string">
            <xs:enumeration value = "Spanish" />
            <xs:enumeration value = "Chinese" />
            <xs:enumeration value = "English" />
            <xs:enumeration value = "German" />
            <xs:enumeration value = "French" />
         </xs:restriction>
       </xs:simpleType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:element>
</xs:schema>

并且错误的位置表示它在-1行-1。

这是我的dtd文件以防万一

<?xml version="1.0"?>
<!DOCTYPE orders [
<!ELEMENT orders (order+)>
<!ELEMENT order (case, batteries, recharger, arm, camera, 
speech)>
<!ELEMENT case (#PCDATA)>
<!ELEMENT batteries (#PCDATA)>
<!ELEMENT recharger (#PCDATA)>
<!ELEMENT arm (reach)>
<!ELEMENT reach (#PCDATA)>
<!ELEMENT camera (#PCDATA)>
<!ELEMENT speech (#PCDATA)>
]>

并且错误没有说明错误来自哪个文件。这是我正在使用的验证器。 http://corefiling.com/opensource/schemaValidate.html

我想因为我不能使用我的dtd,这是我的xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<orders>
  <order>
    <case> Strawberry </case>
    <batteries> 2 </batteries>
    <recharger> 110-120 V </recharger>
    <arm> 2 
       <length> 50 </length>
       <length> 100 </length>
    </arm>
    <camera> 2 </camera>
    <speech> Spanish </speech>
  </order>
  <order>
    <case> Lime </case>
    <batteries> 4 </batteries>
    <recharger> 220-240 V </recharger>
    <arm> 3 
       <length> 75 </length>
       <length> 75 </length>
       <length> 100 </length>
    </arm>
    <camera> 1 </camera>
    <speech> Chinese </speech>
  </order>
  <order>
    <case> Blueberry </case>
    <batteries> 1 </batteries>
    <recharger> 110-120 V </recharger>
    <arm> 2 
       <length> 75 </length>
       <length> 100 </length>
    </arm>
    <camera> 2 </camera>
    <speech> French </speech>
  </order>
</orders>

1 个答案:

答案 0 :(得分:1)

XSD架构无效,你不能拥有像这样的嵌套元素

<xs:element name="orders">
 <xs:element name ="order" maxOccurs="unbounded">

实际上,模式中存在大量错误。 XML Schema很难处理,手工编写它们是不切实际的,我建议使用像Liquid XML Studio这样的graphical XML schema editor

注意: 我认为你的架构应该是这样的: -

An XML Schema Fixed with Liquid XML Studio

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2012 Developer Edition 10.0.1.3941 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="language">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Spanish" />
            <xs:enumeration value="Chinese" />
            <xs:enumeration value="English" />
            <xs:enumeration value="German" />
            <xs:enumeration value="French" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="numOfBat">
        <xs:restriction base="xs:int">
            <xs:enumeration value="1" />
            <xs:enumeration value="2" />
            <xs:enumeration value="3" />
            <xs:enumeration value="4" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="numOfCam">
        <xs:restriction base="xs:int">
            <xs:enumeration value="1" />
            <xs:enumeration value="2" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="caseColor">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Lemonde" />
            <xs:enumeration value="Strawberry" />
            <xs:enumeration value="Lime" />
            <xs:enumeration value="Blueberry" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="reach">
        <xs:restriction base="xs:int">
            <xs:enumeration value="50" />
            <xs:enumeration value="75" />
            <xs:enumeration value="100" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="volt">
        <xs:restriction base="xs:string">
            <xs:enumeration value="110-120" />
            <xs:enumeration value="220-240" />
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="Orders">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="order" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="case" type="caseColor" />
                            <xs:element name="batteries" type="numOfBat" default="1" />
                            <xs:element name="recharger" type="volt" />
                            <xs:element name="arm" />
                            <xs:element name="reaches" type="reach" minOccurs="2" maxOccurs="3" />
                            <xs:element name="camera" type="numOfCam" />
                            <xs:element name="speech" type="xs:language" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>