W3C Schema复杂类型一
我准备在复杂类型里添加一个枚举型的值,但是一直失败
<xs:complexType name = "StaffType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:enumeration value="小明"/>
<xs:enumeration value="小红"/>
<xs:enumeration value="张三"/>
<xs:enumeration value="李四"/>
<xs:enumeration value="王五"/>
<xs:enumeration value="赵六"/>
<xs:attribute name="编号" use="required" type="StaffIDType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
错误信息如下:
File C:\Users\Gigabyte\Desktop\1.xsd is not valid.
Element is not allowed under element .
Reason: The following elements are expected at this location (see below)
Error location: xs:schema / xs:complexType / xs:simpleContent / xs:extension / xs:enumeration
Details
cvc-model-group: Element unexpected by type ‘xs:simpleExtensionType’ of element .
cvc-elt.5.2.1: The element is not valid with respect to the actual type definition ‘xs:simpleExtensionType’.
原因出在,在extension元素里面只能定义以下
<xs:annotation>
<xs:attribute>
<xs:attributeGroup>
<xs:anyAttribute>
这四个元素。
很明显,我们第一段代码是错误的。那怎么解决?
我们把枚举类型的值写到另外一个simpleType里面,然后在complexType的base里选择这个Type。
注意:base指的是填在标签里面的值。
<xs:complexType name = "StaffType">
<xs:simpleContent>
<xs:extension base="StaffNameType">
<xs:attribute name="编号" use="required" type="StaffIDType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="StaffNameType">
<xs:restriction base="xs:string">
<xs:enumeration value="小明"/>
<xs:enumeration value="小红"/>
<xs:enumeration value="张三"/>
<xs:enumeration value="李四"/>
<xs:enumeration value="王五"/>
<xs:enumeration value="赵六"/>
</xs:restriction>
</xs:simpleType>
如果不太理解里面的标签意思,可以参考我的上一篇文章。
http://blog.csdn.net/cg_amaz1ng/article/details/70246988 《XML Schema(W3C)数据类型》