考虑以下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架构规则的一部分......这将是荒谬的。如果扩展类与父类(例如,由元素和属性组成)相比只有一个额外元素,那该怎么办?我是否必须为派生类创建一些属性才能使两个类相似?
我根本不知道我的代码中有什么问题。
答案 0 :(得分:2)
错误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>
和behause_ct的错误,因为xs:属性放错地方,它应该是:
<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>
通过这些更改,架构现在可以编译。
纯元素类型不能扩展混合类型的规则,反之亦然,这些规则是完全明智的规则,但您并没有违反这些规则。