问题
Nested complex XSD elements with type attribute
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 ?.
The XSD parser throws an error :
The element has a type attribute as well as an anonymous child type
Or have I missed something in my understanding?
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>
回答1:
An element's type can be named or anonymous
Intuitively, both together would be impossible because named and anonymous are opposites.
Officially, the exact constraint being violated in the W3C XML Schema Recommendation is src-element.3:
Schema Representation Constraint: Element Declaration Representation 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 and either
<simpleType>
or<complexType>
are mutually exclusive.[1, 2, and 4 elided; see full constraint here]
XSD using named types
<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 using anonymous types
<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>
来源:https://stackoverflow.com/questions/34698770/error-the-element-has-a-type-attribute-as-well-as-an-anonymous-child-type