错误:该元素有一个 type 属性以及一个匿名子类型 [英] Error: The element has a type attribute as well as an anonymous child type
问题描述
只是想弄清楚为什么 XSD 中的复杂元素不能具有类型属性和嵌套的复杂元素?.毕竟类型只是一个用户定义的数据类型,所以应该能够包含任何东西,包括其他用户定义的数据类型?.
Just trying to get my head around why can't a complex element in XSD have a type attribute and a nested complex element in it? . After all type is just a user defined data type and so should be able to contain anything including other user defined data types as well ?.
XSD 解析器抛出错误:
The XSD parser throws an error :
该元素有一个 type 属性以及一个匿名子类型
The element has a type attribute as well as an anonymous child type
或者我在理解中遗漏了什么?
Or have I missed something in my understanding?
那么,如果我必须实现以下 XSD,有可能吗?
So, if I must achieve the below XSD, is it possible?
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="A" type ="A">
<xs:complexType>
<xs:sequence>
<xs:element name="B" type ="B">
<xs:complexType>
<xs:sequence>
<xs:element name="C" type ="C">
<xs:complexType>
<xs:sequence>
<xs:element name="SomeElement" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
推荐答案
元素的类型可以命名为或匿名
直觉上,两者结合起来是不可能的,因为named和anonymous是反对.
An element's type can be named or anonymous
Intuitively, both together would be impossible because named and anonymous are opposites.
官方,在W3C XML Schema 中违反了确切的约束推荐是src-element.3
:
Schema 表示约束:元素声明表示 OK
除了对
元素施加的条件之外模式的模式信息项:以下所有内容都必须是真的:
In addition to the conditions imposed on <element>
element
information items by the schema for schemas: all of the following must
be true:
type 和
或
是互斥的.
[1、2 和 4 被省略;查看完整约束此处]
使用命名类型的 XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="A" type="A"/>
<xs:complexType name="A">
<xs:sequence>
<xs:element name="B" type="B">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="B">
<xs:sequence>
<xs:element name="C" type="C">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="C">
<xs:sequence>
<xs:element name="SomeElement" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
使用匿名类型的 XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:element name="B">
<xs:complexType>
<xs:sequence>
<xs:element name="C">
<xs:complexType>
<xs:sequence>
<xs:element name="SomeElement" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于错误:该元素有一个 type 属性以及一个匿名子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!