当使用XML Schema声明一个complexType
只有一个子元素时,以下三种方式都可以达到目的:
<xs:complexType> <xs:sequence> <xs:element ref="somevalue"/> </xs:sequence> </xs:comlexType>
<xs:complexType> <xs:choice> <xs:element ref="somevalue"/> </xs:choice> </xs:comlexType>
<xs:complexType> <xs:all> <xs:element ref="somevalue"/> </xs:all> </xs:comlexType>
显然,sequence
、choice
和all
不是单个元素所必需的,因为它们应该用于指示顺序的多个元素。 是否有更简洁的方法来声明只有一个子元素的 complexType
?(即消除使用 sequence
的方法,all
或 choice
,不知何故。)
最佳答案
只是消除 xs:sequence
、xs:choice
或 xs:all
不是一种选择:
<xs:complexType name="cType">
<xs:element name="e"/>
</xs:complexType>
无效。
参见 XML Representation of Complex Type Definitions其中complexType
的内容模型定义如下:
(annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))
没有规定 element
是 complexType
的直接子级。
关于xml - 如何声明一个 complexType 只有一个子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223099/