2012-12-04 26 views
4

我继承了一些2004年最后更新的XML模式。自那时起IT一直在工作,但软件更新已导致一些问题。 xml不再在XMLspy中验证。XML Schema选择不同序列中的不同类型的相同元素导致类型错误

无论如何,问题似乎是通过定义具有不同名称的几个不同simpleType来定义元素的当前方式是不正确的。我不明白如何解决这个问题。

用XMLspy 2004打开文件xml验证就好了。

开口与lastes XMLSPY我收到以下错误的文件:

Element 'Element' is not consistent with element 'Element'. 
    Details: 
     cos-element-consistent.2: Both type definitions ('type2' and 'type1') must have the same name 

注:以下XML /模式不是实际 XML。我重新创建了它,并将其推广以防止出现任何类型的问题(在这里发布实时程序的内部工作有点不合时宜)。所以,如果你看到一个错字,这是我的错,而不是Schema的错。

的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <FirstElement></FirstElement> 
    <SecondElement> 
     <Element></Element> 
     <Parameters> 
      <Param value="#Value#">name1</Param> 
     </Parameters> 
    </SecondElement> 
</Package> 

他们给我的XML模式,它产生错误:

<?ml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Package"> 
     <xs:complexType> 
      <xs:choice> 
       <xs:sequence> 
        <xs:element name="FirstElement"/> 
        <xs:element name="SecondElement"> 
         <xs:complexType> 
          <xs:group ref="Group1"/> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
       <xs:sequence> 
        <xs:element name="FirstElement"/> 
        <xs:element name="SecondElement"> 
         <xs:complexType> 
          <xs:group ref="Group2" /> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
      </xs:choice> 
     </xs:complextype> 
    </xs:element> 
    <xs:group name="Group1"> 
     <xs:choice> 
      <xs:sequence> 
       <xs:element name="Element" type="type1"/> 
      </xs:sequence> 
      <xs:sequence> 
       <xs:element name="Element" type="type2"/> 
      </xs:sequence> 
      <xs:sequence> 
       <xs:element name="Element" type="type3"/> 
      </xs:sequence> 
     </xs:choice> 
    </xs:group> 
    <xs:group name="Group2"> 
     <xs:choice> 
      <xs:sequence> 
       <xs:element name="Element" type="type1"/> 
      </xs:sequence> 
      <xs:sequence> 
       <xs:element name="Element" type="type4"/> 
       <xs:element name="Parameters" minOccurs="0"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="Param"> 
           <xs:simpleType> 
            <xs:restriction base="xs:string"> 
             <xs:enumeration value="TRUE"/> 
             <xs:enumeration value="FALSE"/> 
            </xs:restriction> 
           </xs:simpleType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:choice> 
    </xs:group> 

    <xs:simpleType name="type1"> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="ONE"/> 
     </xs:restriction> 
    </xs:simpleType> 
    <xs:simpleType name="type2"> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="TWO"/> 
     </xs:restriction> 
    </xs:simpleType> 
    <xs:simpleType name="type3"> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="THREE"/> 
     </xs:restriction> 
    </xs:simpleType> 
    <xs:simpleType name="type4"> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="FOUR"/> 
     </xs:restriction> 
    </xs:simpleType> 
</xs:schema> 

这将是真棒,如果有人给我一些有识之士就如何保持逻辑模式在使XML有效的同时进行检查。

谢谢!

+1

你进入了“共现约束”的复杂世界。这是您想要根据元素的同胞来限制元素的位置。有几种方法:XSL错误解析,Schematron和XML Schema 1.1。最简单的做法是将约束作为业务逻辑放在应用程序中。 http://www.w3.org/wiki/Co-occurrence_constraints –

回答

0

将David W的评论发布为答案,以提高新会员或访问者的可读性。

You're into the complex world of "co-occurrence constraints". It's where you want to restrict an element based on the element's siblings. There are a few methods: XSL error parsing, Schematron, and XML Schema 1.1. The easiest thing to do is put the constraints as business logic in the application.
And Here is a wiki Link for further info.

相关问题