[疯狂xml讲义]chap4之Schema记录

1.内置字符串类型

string.xsd:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!--W3C Schema generated by XMLSpy v2008 rel. 2 sp2 (http://www.altova.com)-->
 3 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 4     <xs:complexType name="strlist_Type">
 5         <xs:sequence>
 6             <xs:element ref="string"/>
 7             <xs:element ref="normalizedString"/>
 8             <xs:element ref="token"/>
 9             <xs:element ref="name" maxOccurs="unbounded"/>
10             <xs:element ref="ncname" maxOccurs="unbounded"/>
11             <xs:element ref="qname" maxOccurs="unbounded"/>
12         </xs:sequence>
13     </xs:complexType>
14     <xs:element name="strlist" type="strlist_Type"/>
15     <xs:element name="token" type="xs:token"/>
16     <xs:element name="string" type="xs:string"/>
17     <xs:element name="normalizedString" type="xs:normalizedString"/>
18     <xs:element name="name" type="xs:Name"/>
19     <xs:element name="ncname" type="xs:NCName"/>
20     <xs:element name="qname" type="xs:QName"/>
21 </xs:schema>

注:这里定义了一个全局的自定义数据类型strlist_Type,元素strlist使用了。

string.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <strlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:noNamespaceSchemaLocation="string.xsd">
 4     <string>    Java      xml    Ajax    </string>
 5     <normalizedString>     Java      xml    Ajax    </normalizedString>
 6     <token>    Java      xml    Ajax    </token>
 7     <!-- 下面name元素的类型是Name类型 -->
 8     <name>a12</name>
 9     <name>a-b</name>
10     <name>a_b</name>
11     <name>a:b</name>
12     <name>a.b</name>
13     <name>:b</name>
14     <!-- NCName类型的元素  -->
15     <ncname>_b</ncname>
16     <ncname>a-x</ncname>
17     <!-- QName类型的值可以省略命名空间前缀。 -->
18     <qname>a-x</qname>
19     <!-- 如果QName类型的值使用了命名空间前缀,
20         则该命名空间前缀必须有对应的命名空间。 -->
21     <qname xmlns:java="http://www.crazyit.org">java:a-x</qname>
22 </strlist>

2.数值类型

number.xsd:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 3     <xs:complexType name="numberList_Type">
 4         <xs:sequence>
 5             <xs:element ref="float" maxOccurs="unbounded"/>
 6             <xs:element ref="double" maxOccurs="unbounded"/>
 7             <xs:element ref="decimal" maxOccurs="unbounded"/>
 8             <xs:element ref="integer" maxOccurs="unbounded"/>
 9             <xs:element ref="int" maxOccurs="unbounded"/>
10             <xs:element ref="positiveInteger" maxOccurs="unbounded"/>
11         </xs:sequence>
12     </xs:complexType>
13     <xs:element name="numberList" type="numberList_Type"/>
14     <xs:element name="float" type="xs:float"/>
15     <xs:element name="double" type="xs:double"/>
16     <xs:element name="decimal" type="xs:decimal"/>
17     <xs:element name="integer" type="xs:integer"/>
18     <xs:element name="int" type="xs:int"/>
19     <xs:element name="positiveInteger" type="xs:positiveInteger"/>
20 </xs:schema>

number.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <numberList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3      xsi:noNamespaceSchemaLocation="number.xsd">
 4     <float>5</float>
 5     <float>5.6</float>
 6     <float>4.12256229</float>
 7     <!-- 下面正0大于负0 -->
 8     <float>+0</float>
 9     <float>-0</float>
10     <!-- 负无穷大 -->
11     <float>-INF</float>
12     <!-- 下面实际上是500  -->
13     <double>5e2</double>
14     <!-- 下面实际上是0.56  -->
15     <double>5.6E-1</double>
16     <!-- 下面实际上是20  -->
17     <double>.2e2</double>
18     <double>4.12256229</double>    
19     <!-- 正无穷大 -->
20     <double>INF</double>
21     <double>NaN</double>
22     <decimal>1.2</decimal>
23     <decimal>0.2</decimal>
24     <decimal>.89</decimal>
25     <decimal>+0</decimal>
26     <integer>+0</integer>
27     <integer>-0</integer>
28     <!-- integer类型的值没有限制  -->
29     <integer>99999999999999999999999999999999</integer>
30     <!-- int类型的值有限制,下面分别是最大值和最小值 -->
31     <int>2147483647</int>
32     <int>-2147483648</int>
33     <!-- positiveInteger类型的值没有最大限制 -->
34     <positiveInteger>99999999999999999999999999999999</positiveInteger>
35     <!-- positiveInteger类型的值最小为1 -->
36     <positiveInteger>1</positiveInteger>
37 </numberList>

3.日期。时间类型

date.xsd:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 3     <xs:complexType name="date_list_Type">
 4         <xs:sequence>
 5             <xs:element ref="date" maxOccurs="unbounded"/>
 6             <xs:element ref="date_time" maxOccurs="unbounded"/>
 7             <xs:element ref="gyear" maxOccurs="unbounded"/>
 8             <xs:element ref="gyear_month" maxOccurs="unbounded"/>
 9             <xs:element ref="gmonth" maxOccurs="unbounded"/>
10             <xs:element ref="gmonth_day" maxOccurs="unbounded"/>
11             <xs:element ref="gday" maxOccurs="unbounded"/>
12             <xs:element ref="duration" maxOccurs="unbounded"/>
13         </xs:sequence>
14     </xs:complexType>
15     <xs:element name="date_list" type="date_list_Type"/>
16     <xs:element name="date" type="xs:date"/>
17     <xs:element name="date_time" type="xs:dateTime"/>
18     <xs:element name="gyear" type="xs:gYear"/>
19     <xs:element name="gyear_month" type="xs:gYearMonth"/>
20     <xs:element name="gmonth" type="xs:gMonth"/>
21     <xs:element name="gmonth_day" type="xs:gMonthDay"/>
22     <xs:element name="gday" type="xs:gDay"/>
23     <xs:element name="duration" type="xs:duration"/>
24 </xs:schema>

date.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <date_list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3     xsi:noNamespaceSchemaLocation="date.xsd">
 4     <date>1992-02-03</date>
 5     <!-- 添加负号表示公元前 -->
 6     <date>-1992-12-23</date>
 7     <!-- 添加Z后缀表示UTC时间 -->
 8     <date>-1992-12-23Z</date>
 9     <!-- 既包含日期、也包含时间 -->
10     <date_time>-1992-12-23T08:06:19.123</date_time>
11     <!-- 毫秒数只有5 -->
12     <date_time>-1992-12-23T11:56:19.5Z</date_time>
13     <!-- 添加负号表示公元前 -->
14     <gyear>-2009Z</gyear>
15     <!-- 下面是2个有效的gYearMonth类型的值 -->
16     <gyear_month>2009-04Z</gyear_month>
17     <gyear_month>-2009-12Z</gyear_month>
18     <!-- 下面是2个有效的gMonth类型的值 -->
19     <gmonth>--05Z</gmonth>
20     <gmonth>--12Z</gmonth>
21     <!-- 下面是1个有效的gMonthDay类型的值 -->    
22     <gmonth_day>--12-03Z</gmonth_day>
23     <!-- 下面是2个有效的gDay类型的值 -->    
24     <gday>---15</gday>
25     <gday>---15Z</gday>
26     <!-- 下面是4个有效的duration类型的值 -->    
27     <!-- 代表12年 -->
28     <duration>P12Y</duration>
29     <!-- 代表13天再加3分钟 -->
30     <duration>P13DT3M</duration>
31     <!-- 代表12年再加0.3秒 -->
32     <duration>P12YT0.3S</duration>
33     <!-- 代表0.3秒 -->
34     <duration>PT0.3S</duration>    
35 </date_list>

4.布尔类型

boolean.xsd:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 3     <xs:complexType name="boolean_list_Type">
 4         <xs:sequence>
 5             <xs:element ref="boolean" maxOccurs="unbounded"/>
 6         </xs:sequence>
 7     </xs:complexType>
 8     <xs:element name="boolean_list" type="boolean_list_Type"/>
 9     <xs:element name="boolean" type="xs:boolean"/>
10 </xs:schema>

boolean.xml:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <boolean_list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
3     xsi:noNamespaceSchemaLocation="boolean.xsd">
4     <!-- 如下是4个有效的boolean值 -->
5     <boolean>true</boolean>
6     <boolean>false</boolean>
7     <boolean>0</boolean>
8     <boolean>1</boolean>
9 </boolean_list>

5.anyURI类型

uri.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <uri_list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:noNamespaceSchemaLocation="uri.xsd">
 4     <!-- 下面是两个有效的URL -->
 5     <uri>http://www.crazyit.org</uri>
 6     <uri>http://www.crazyit.org/ethos.php</uri>
 7     <!-- 下面是有效的URL加上锚点 -->
 8     <uri>http://www.crazyit.org/ethos.php#name</uri>
 9     <!-- 下面代表访问文件系统的URI -->
10     <uri>file:///g:/ss</uri>
11     <uri>abc.txt</uri>
12 </uri_list>

uri.xsd:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 3     <xs:complexType name="uri_list_Type">
 4         <xs:sequence>
 5             <xs:element ref="uri" maxOccurs="unbounded"/>
 6         </xs:sequence>
 7     </xs:complexType>
 8     <xs:element name="uri_list" type="uri_list_Type"/>
 9     <xs:element name="uri" type="xs:anyURI"/>
10 </xs:schema>

6.binary类型

binary.xml:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <binaryList  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3      xsi:noNamespaceSchemaLocation="binary.xsd">
4     <!-- hexBinary类型的值必须是偶数位的十六进制数 --> 
5     <wawa>1f12fa12aefa</wawa>
6     <!-- base64Binary类型的值必须是4n位的字符 -->
7     <haha>abcdxyz+</haha>
8 </binaryList>

binary.xsd:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 3     <xs:complexType name="binaryList_Type">
 4         <xs:sequence>
 5             <xs:element ref="wawa"/>
 6             <xs:element ref="haha"/>
 7         </xs:sequence>
 8     </xs:complexType>
 9     <xs:element name="binaryList" type="binaryList_Type"/>
10     <xs:element name="wawa" type="xs:hexBinary"/>
11     <xs:element name="haha" type="xs:base64Binary"/>
12 </xs:schema>
posted @ 2012-11-27 09:11  xcf007  阅读(208)  评论(0编辑  收藏  举报