Tuesday, October 7, 2008

xs:union

xs:union 定义了联合类型,比如我们要求一个元素的内容既可以是整数也可以是日期类型,那么我们可以这样:
<xs:simpleType name="integerOrData">
<xs:union memberTypes="xs:integer xs:date"/>
</xs:simpleType>
你也可以这样写:
<xs:simpleType name="myIntegerUnion">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:integer"/>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="undefined"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>

xs:list

xs:list 用于定义一些列用空格分开的元素,比如下面定义一个类型,他的内容限制为是一系列用空格分开的整型。
<xs:simpleType name="integerList">
<xs:list itemType="xs:integer"/>
</xs:simpleType>

你还可以给他加上其他限制,比如最大不能超过100.

<xs:simpleType name="myIntegerList">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>

xs:fractionDigits

xs:fractionDigits 定义了一个数字的小数位是几位,比如
<xs:simpleType name="fractionDigits">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>

它定义了这个数字的小数位是2为,1.12是允许的,1.111就不允许了。

xs:totalDigits

xs:totalDigits 定义了数字是多少位,比如

<xs:simpleType name="totalDigits">
<xs:restriction base="xs:integer">
<xs:totalDigits value="5"/>
</xs:restriction>
</xs:simpleType>

xs:minExclusive 和 xs:minInclusive

xs:minExclusive 和 xs:minInclusive 定义了最小值不能小于,和最小值是什么,比如:
<xs:simpleType name="strictlyPositive">
<xs:restriction base="xs:double">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>



<xs:simpleType name="positive">
<xs:restriction base="xs:double">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>

xs:maxExclusive 和xs:maxInclusive

xs:maxExclusive 和xs:maxInclusive限定了一个类型的不能超过和最大值是多少,比如下面的例子:
<xs:simpleType name="maxExclusive">
<xs:restriction base="xs:float">
<xs:maxExclusive value="10"/>
</xs:restriction>
</xs:simpleType>

这个自定义属性限制了他的值最大不能超过10,9.99999999是允许的,但是10 就不允许了。

<xs:simpleType name="thousands">
<xs:restriction base="xs:double">
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>

这个自定义属性限制了他的值是10,9.99999999是允许的,并且10也是允许的。

xs:pattern

xs:pattern 可以通过正则表达式的方式,对定义类型的值进行判断,以校验其合法性,比如下面的例子定义了URI类型必须是以http://开头的值:
<xs:simpleType name="httpURI">
<xs:restriction base="xs:anyURI">
<xs:pattern value="http://.*"/>
</xs:restriction>
</xs:simpleType>

xs:maxLength和xs:minLength

xs:maxLength定义了类型的最大长度,例如:

<xs:simpleType name="binaryImage">
<xs:restriction base="xs:hexBinary">
<xs:maxLength value="1024"/>
</xs:restriction>
</xs:simpleType>

同理xs:minLength定义了类型的最小长度,例如:
<xs:simpleType name="longName">
<xs:restriction base="xs:NCName">
<xs:minLength value="6"/>
</xs:restriction>
</xs:simpleType>

xs:length

xs:length 用来限制元素的长度
比如:
<xs:simpleType name="standardNotations">
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>

他定义了这个字符串的长度只能是8位

xs:enumeration

xs:enumeration 定义了一个枚举的类型,比如我们定义一个新的URI类型,这个类型限制他的元素值只能包含下列的三个值,
http://www.w3.org/TR/xmlschema-0/
http://www.w3.org/TR/xmlschema-1/
http://www.w3.org/TR/xmlschema-2/
我们就需要想下面这样定义:
<xs:simpleType name="schemaRecommendations">
<xs:restriction base="xs:anyURI">
<xs:enumeration value="http://www.w3.org/TR/xmlschema-0/"/>
<xs:enumeration value="http://www.w3.org/TR/xmlschema-1/"/>
<xs:enumeration value="http://www.w3.org/TR/xmlschema-2/"/>
</xs:restriction>
</xs:simpleType>

XSD 中的Restriction

在我们需要自定义类型的时候,Restriction 是必不可少的。下面是一个例子

<xs:simpletype name="myInteger">
<xs:restriction base="xs:integer">
<xs:mininclusive value="-2">
<xs:maxexclusive value="5">
</xs:maxexclusive>
</xs:mininclusive>
</xs:restriction></xs:simpletype>

这个例子告诉我们我们要自定义一个新的类型,名字为myInteger,restriction子元素说明我们要给这个新的自定义类型加上一些限制, base 属性说明了,这个新的类型继承了integer基本类型。好了,然后mininclusive 定义了这个整数最小两位数,maxexclusive 定义了最多不超过3位数。
一旦定义好这个简单类型我们就可以在其他地方引用这个类型,比如

<xs:element name="cost" type="myInteger"/>
好了后面的文章我们会介绍其他的“限制” 类型。