1

我知道我已经多次发布了这个问题。但我现在遇到了一个新错误,我需要帮助。这是我收到的错误。“s4s-elt-invalid-content.1:‘#AnonType_orders’的内容无效。元素‘element’无效、错位或出现频率过高。”

这是我的 xsd 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="reach">
        <xs:restriction base = "xs:integer">
          <xs:enumeration value = "50" />
          <xs:enumeration value = "75" /> 
          <xs:enumeration value = "100" />
         </xs:restriction>
 </xs:simpleType>
<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="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="numOfBat">
    <xs:restriction base="xs:integer">
        <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:string">
        <xs:pattern value="1|2" />
    </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:element name ="order" maxOccurs="unbounded"> 
<xs:complexType>
 <xs:sequence>
    <xs:element name="custName" type ="xs:string" />
                <xs:attribute name ="custID" />
<xs:element name="case" type="xs:caseColor" >
</xs:element>
<xs:element name="batteries" type="xs:numOfBat" default = 
"1"> 
</xs:element>
<xs:element name="recharger" type="xs:volt" /> 
<xs:element name="arm"> 
   <xs:element name ="reaches" minOccurs="2" maxOccurs="3" 
type="xs:reach" />
</xs:element>
<xs:element name ="camera" type="xs:numOfCam" /> 
<xs:element name = "speech" type="xs:language" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<custName custID="11223"> John Doe </custName>
<case> Strawberry </case>
<batteries> 2 </batteries>
<recharger> 110-120 V </recharger>
<arm>  
   <reach> 50 </reach>
   <reach> 100 </reach>
</arm>
<camera> 2 </camera>
<speech> Spanish </speech>
</order>
<order>
<custName custID="45392"> Jane Camp </custName>
<case> Lime </case>
<batteries> 4 </batteries>
<recharger> 220-240 V </recharger>
<arm> 3
   <reach> 75 </reach>
   <reach> 75 </reach>
   <reach> 100 </reach>
</arm>
<camera> 1 </camera>
<speech> Chinese </speech>
</order>
<order>
<custName custID="69482"> George Carlton </custName>
<case> Blueberry </case>
<batteries> 1 </batteries>
<recharger> 110-120 V </recharger>
<arm> 
   <reach> 75 </reach>
   <reach> 100 </reach>
</arm>
<camera> 2 </camera>
<speech> French </speech>
</order>
</orders>
4

2 回答 2

3

xsd的形成很糟糕,特别是你的orders元素。您还为自定义类型引用了错误的命名空间。我会解决我看到的问题。

首先:您的orders节点被定义为 axs:complexType并且您有一个order元素作为 的complexType子节点,但 element 不是允许的子节点。您需要在两者之间放置xs:sequencexs:all

<xs:element name="orders">
  <xs:complexType>
    <xs:sequence>
      <xs:element name ="order" maxOccurs="unbounded">
         <!-- existing nodes -->
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

下一步:您的arm元素有 1 个子元素,但xs:element在此上下文中是不允许的。您需要将其定义为complexType然后sequenceall再次。

<xs:element name="arm">
    <xs:complexType>
        <xs:sequence>
            <xs:element name ="reaches" minOccurs="2" maxOccurs="3" type="reach" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

第三:您的custId属性未在正确的位置定义。 xs:attributes必须将其定义为元素下的子节点,其中它们被定义为属性,但是 xs:simpleType 不能有属性节点,因此您需要将其定义为 complexType。这是您的问题的可能解决方案:

<xs:element name="custName">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
               <xs:attribute name="custID" type="xs:int" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

最后:当您引用所有自定义类型(reachnumOfBatvolt等)时,您将它们作为xs:命名空间的一部分进行引用,但是这些类型并未在xs:命名空间中定义。由于您没有为 XSD 定义 targetNamespace,因此您的自定义类型将成为默认命名空间的一部分。您只需xs:要从类型中删除 。

例如,您应该将它们声明为:type="reach"、、、type="numOfBat"type="caseColor"

于 2012-04-11T14:41:30.963 回答
0

我不知道您使用什么工具进行验证,但它产生的消息似乎毫无帮助。有关信息,以下是撒克逊人产生的消息:

Saxon-EE 9.5.0.1J from Saxonica
Java version 1.6.0_27
Using license serial number M008277
Loading schema document file:/Users/mike/Desktop/temp/test.xsd
Error at xs:element on line 48 column 49 of test.xsd:
  Element xs:element cannot appear here: expected one of {choice, sequence, assert,
  openContent, annotation, attributeGroup, anyAttribute, simpleContent, all, attribute,
  group, complexContent}
Error at xs:attribute on line 52 column 48 of test.xsd:
  Element <xs:attribute> is not allowed as a child of <xs:sequence>
Error at xs:element on line 61 column 19 of test.xsd:
  Element xs:element cannot appear here: expected one of {annotation, simpleType, key,
  complexType, keyref, unique, alternative}
Error at xs:element on line 61 column 19 of test.xsd:
  Element <xs:element> is not allowed as a child of <xs:element>
Schema processing failed: 4 errors were found while processing the schema

我希望你发现这些更清楚。本质上,问题在于 xs:element 不能作为 xs:complexType 的子项出现:您可能需要介于两者之间的 xs:sequence 之类的东西。

于 2012-04-09T18:19:01.063 回答