xsd 错误信息:"The content type of a derived type and that of its base must both be mixed or both be element-only."

xsd error message: "The content type of a derived type and that of its base must both be mixed or both be element-only."

考虑以下 xml 模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
    vc:minVersion="1.0" vc:maxVersion="1.1">


    <xs:element name="zoo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/> 
                <xs:element name="zootier" type="tier" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>



    <xs:complexType name="tier">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/> 
            <xs:element name="alter" type="xs:positiveInteger"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID"/>
    </xs:complexType>

    <xs:complexType name="säugetier_ct">
        <xs:complexContent>
            <xs:extension base="tier">
                <xs:sequence>
                    <xs:element name="tragezeit" type="xs:positiveInteger"/>  
                </xs:sequence>   
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="pferd_ct">
        <xs:complexContent>
            <xs:extension base="tier">
                    <xs:attribute name="schimmel" type="xs:boolean"/>              
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="katze_ct">
        <xs:complexContent>
            <xs:sequence>
                <xs:element name="fellfarbe" type="xs:string"/>  
            </xs:sequence>   
        </xs:complexContent>
    </xs:complexType>


    <xs:complexType name="vogel_ct">
        <xs:complexContent>
            <xs:extension base="tier">
                <xs:attribute name="flugfaehig" type="xs:boolean"/>                   
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="gans_ct">
        <xs:complexContent>
            <xs:extension base="tier">
                <xs:sequence>
                    <xs:element name="schlachtgewicht" type="xs:double"/>  
                </xs:sequence>            
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="fink_ct">
        <xs:complexContent>
            <xs:sequence>
                <xs:element name="beringt" type="xs:boolean"/>   
            </xs:sequence>
        </xs:complexContent>
    </xs:complexType>



    <xs:complexType name="pfleger">
        <xs:sequence>
            <xs:element name="pflegt" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="tier" type="xs:IDREF"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>



    <xs:complexType name="behausung_ct">
        <xs:attribute name="name" type="xs:string"/>
        <xs:sequence>  
            <xs:element name="zootier" type="tier" maxOccurs="unbounded"/>
        </xs:sequence>      
    </xs:complexType>

    <xs:complexType name="gebäude_ct">
        <xs:complexContent>
            <xs:extension base="behausung_ct">
                <xs:sequence>
                    <xs:element name="flaeche" type="xs:double"/> 
                </xs:sequence>     
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="teich_ct">
        <xs:complexContent>
            <xs:extension base="behausung_ct">
                <xs:sequence>
                    <xs:element name="wassertiefe" type="xs:double"/> 
                </xs:sequence>  
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>



</xs:schema>

这给了我以下错误信息:

Error1: The content of 'katze_ct' is invalid.  Element 'sequence' is invalid, misplaced, or occurs too often.

Error2: The content of 'fink_ct' is invalid.  Element 'sequence' is invalid, misplaced, or occurs too often.

Error3: The content of 'behausung_ct' is invalid.  Element 'sequence' is invalid, misplaced, or occurs too often.

Error 4: The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'gebäude_ct' is element only, but its base type is not.

Error 5: The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'teich_ct' is element only, but its base type is not.

错误 1-3 属于同一类型。错误 4 和 5 也是同一类型。 关于错误 4+5:为什么派生类型必须与父类型具有相同类型的内容?这不能成为 xml 模式规则的一部分……这将是荒谬的。如果扩展 class 与父 class 相比只有一个附加元素(例如,由元素和属性组成)怎么办?我是否必须为派生的 class 发明一些属性才能使两个 class 相似?

我根本不明白我的代码有什么问题。

错误 1-3 是有道理的,错误 4-5 是虚假的。当您解决前三个问题时,它们可能会消失。编译器在发现错误后尝试继续时走奇怪的路并不罕见。

使用 Saxon 验证模式时,我得到了 katze_ct 和 fink_ct 的错误,可以通过删除虚假的 complexContent 元素来修复这些错误:

<xs:complexType name="katze_ct">
    <xs:sequence>
        <xs:element name="fellfarbe" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

和 behausing_ct 的错误,因为 xs:attribute 放错了位置,应该是:

<xs:complexType name="behausung_ct">      
        <xs:sequence>
            <xs:element name="zootier" type="tier" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="name" type="xs:string"/>
    </xs:complexType>

有了这些更改,架构现在可以编译了。

element-only 类型不能扩展混合类型(反之亦然)的规则是非常明智的规则,但您没有违反它们。