XML学习之Xml Schema:四、自定义简单类型

        简单类型可以分为:原子类型、列表类型、联合类型。

        列表类型的值由原子类型组成,各个值之间有空格分隔。要定义新的列表类型,需要使用xs:list元素,该元素的itemType属性可以引用一个现有的原子类型,或者在xs:list中使用xs:simpleType子元素来指定匿名的类型。

        限制列表类型。有5个面可以适用于列表类型,分别是:length、mingLength、maxLength、enumeration、pattern。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="zhixiashi">
		<xs:simpleType>
			<xs:list itemType="xs:token"/>
		</xs:simpleType>
	</xs:element>
</xs:schema>


<zhixiashi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xs1.xml">北京市 天津市 上海市</zhixiashi>

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="email" type="emailsType"/>
	<xs:simpleType name="emailsType">
		<xs:restriction>
			<xs:simpleType>
				<xs:list itemType="xs:token"/>
			</xs:simpleType>
			<xs:minLength value="4"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmls:xs="http://www.w3.org/2001/XMLSchema">
	<xs:list>
		<xs:simple>
			<xs:restriction base="xs:integet">
				<xs:enumeration value="1"/>
				<xs:enumeration value="2"/>
				<xs:enumeration value="3"/>
				<xs:enumeration value="4"/>
			</xs:restriction>
		</xs:simple>
	</xs:list>
</xs:schema>


         联合类型可以包含多个原子类型或者列表类型。定义联合类型,需要使用xs:union元素,该元素的memberTypes属性给出组成联合类型的所有成员类型。各个成员类型之间以空白字符分隔,或者你可以在xs:union元素内使用一个或多个xs:simple子元素来指定匿名的成员类型。限制联合类型中有2个面可以应用了联合类型,分别是enumeration和pattern。

<?xml version="1.0" encoding="UTF-8"?>
<xs:element name="employee">
	<xs:simpleType>
		<xs:union memberType="xs:integer xs:token"/>
	</xs:simpleType>
</xs:element>

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="number" type="numberType"/>
	<xs:simpleType name="numberType">
		<xs:restriction>
			<xs:simple>
				<xs:union memberType="xs:integer xs:token"/>
			</xs:simple>
			<xs:enumeration value="1"/>
			<xs:enumeration value="2"/>
			<xs:enumeration value="I"/>
			<xs:enumeration value="II"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

版权声明:本文为tuolingss原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。