[xsd学习]xsd实例

以下为一个表示学校的xml文件,学校内有若干学生,每个学生都有基本信息,电脑信息,选课信息

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 表示一个学校的xml -->

<school 
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com school.xsd">
  
    <!-- 表示学校学生,可以有一个或者多个 -->
    <students>
        <!-- 每个学生都有学号、姓名、年龄,
        每个学生都有选课,有些选多门功课
        有些学生有电脑 -->
        <student>
            <id address="北京">101</id>
            <name>张三</name><!-- 姓名不能为空 -->
            <age>20</age><!--年龄要求必须是20-30之间 -->
            <class>语文</class>
        </student>
        <student>
            <id address="上海">201</id>
            <name>张三</name><!-- 姓名不能为空 -->
            <age>20</age><!--年龄要求必须是20-30之间 -->
            <computer>IBM</computer>
            <class>语文</class>
            <class>语文</class>
        </student>
    </students>
</school>

xml引入school.xsd文件作文校验,目前主要的xsd写法有两种:

1、使用元素为基本对象,基于首先对所有元素和属性的定义,然后再使用 ref 属性来引用它们。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 3   targetNamespace="http://www.w3schools.com"
 4   xmlns="http://www.w3schools.com"
 5   elementFormDefault="qualified">
 6 
 7   
 8   <xs:element name="id">
 9     <xs:complexType ><!--定义学号为数字型,且只能为3位数字-->
10       <xs:simpleContent>
11         <xs:extension base="xs:integer" >
12           <xs:attribute name="address" type="xs:string"></xs:attribute><!--定义属性-->
13         </xs:extension>
14       </xs:simpleContent>
15     </xs:complexType >
16   </xs:element>
17   <xs:element name="name">
18       <xs:simpleType><!--定义姓名-->
19         <xs:restriction base="xs:string" />
20       </xs:simpleType>
21   </xs:element>
22   <xs:element name="sex"> 
23       <xs:simpleType ><!--定义性别,只能为男或者女-->
24         <xs:restriction base="xs:string" >
25           <xs:pattern value="男|女"></xs:pattern>
26         </xs:restriction>    
27       </xs:simpleType>
28   </xs:element>
29   <xs:element name="age">
30       <xs:simpleType><!--定义年龄,只能为20-30的整数-->
31         <xs:restriction base="xs:integer" >
32           <xs:minInclusive value="20" />
33           <xs:maxInclusive value="30" />
34         </xs:restriction>
35       </xs:simpleType>
36   </xs:element>
37   <xs:element name="computer">
38       <xs:simpleType ><!--定义电脑,只能包含字母-->
39         <xs:restriction base="xs:string">
40           <xs:pattern value="[a-z]+"></xs:pattern>
41         </xs:restriction>
42       </xs:simpleType>
43   </xs:element>
44   <xs:element name="class">
45   <xs:simpleType><!--定义课程,只能为选定值-->
46     <xs:restriction base="xs:string">
47       <xs:enumeration value="语文" />
48       <xs:enumeration value="数学" />
49       <xs:enumeration value="政治" />
50     </xs:restriction>
51   </xs:simpleType>
52   </xs:element>
53   <xs:group name="studentGroup">
54     <xs:sequence><!--表明元素可以按任意属性出现,且只能出现一次-->
55       <xs:element ref="id" />
56       <xs:element ref="name" />
57       <xs:element ref="age" />
58       <xs:element ref="sex"/>
59     </xs:sequence>
60   </xs:group>
61   <xs:element name="student">    
62       <xs:complexType>
63         <xs:sequence>
64           <xs:group ref="studentGroup"></xs:group><!--引用学生基本属性-->
65           <xs:element name="computer" type="computerType"></xs:element><!--学生拥有电脑-->
66           <xs:any></xs:any><!--其他属性-->
67         </xs:sequence>
68       </xs:complexType>
69   </xs:element>
70   <xs:element name="students">
71       <xs:complexType>
72         <xs:sequence>
73           <xs:element ref="student" minOccurs="1" maxOccurs="10">
74           </xs:element>
75         </xs:sequence>
76         <xs:attribute name="address" type="xs:string"></xs:attribute><!--定义属性-->
77       </xs:complexType>
78   </xs:element>
79   
80   <xs:element name="school">  
81       <xs:complexType>
82         <xs:sequence>
83           <xs:element ref="students" />
84         </xs:sequence>
85       </xs:complexType>
86   </xs:element>
87 
88 </xs:schema>
View Code

2、使用类型为基本对象,定义了类或者类型,这样使我们有能力重复使用元素的定义。具体的方式是:首先对简易元素和复合元素进行命名,然后通过元素的 type 属性来指向它们

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 3   targetNamespace="http://www.w3schools.com"
 4   xmlns="http://www.w3schools.com"
 5   elementFormDefault="qualified">
 6   
 7   <xs:simpleType name="addressType"><!--地址属性-->
 8     <xs:restriction base="xs:string">
 9     </xs:restriction>
10   </xs:simpleType>
11   
12   <xs:complexType  name="idType"><!--定义学号为数字型,且只能为3位数字-->
13     <xs:simpleContent>
14     <xs:extension base="xs:integer" >
15       <xs:attribute name="address" type="addressType"></xs:attribute><!--定义属性-->
16     </xs:extension>
17     </xs:simpleContent>
18   </xs:complexType >
19   <xs:simpleType name="nameType"><!--定义姓名-->
20     <xs:restriction base="xs:string" />
21   </xs:simpleType>
22   <xs:simpleType name="sexType"><!--定义性别,只能为男或者女-->
23     <xs:restriction base="xs:string" >
24       <xs:pattern value="男|女"></xs:pattern>
25     </xs:restriction>    
26   </xs:simpleType>
27   <xs:simpleType name="ageType"><!--定义年龄,只能为20-30的整数-->
28     <xs:restriction base="xs:integer" >
29       <xs:minInclusive value="20" />
30       <xs:maxInclusive value="30" />
31     </xs:restriction>
32   </xs:simpleType>
33   <xs:simpleType name="computerType"><!--定义电脑,只能包含字母-->
34     <xs:restriction base="xs:string">
35       <xs:pattern value="[a-z]+"></xs:pattern>
36     </xs:restriction>
37   </xs:simpleType>
38   <xs:simpleType name="classType"><!--定义课程,只能为选定值-->
39     <xs:restriction base="xs:string">
40       <xs:enumeration value="语文" />
41       <xs:enumeration value="数学" />
42       <xs:enumeration value="政治" />
43     </xs:restriction>
44   </xs:simpleType>
45   <xs:group name="studentGroup">
46     <xs:sequence><!--表明元素可以按任意属性出现,且只能出现一次-->
47       <xs:element name="id" type="idType" />
48       <xs:element name="name" type="nameType" />
49       <xs:element name="age" type="ageType" />
50       <xs:element name="sex" type="sexType" />
51     </xs:sequence>
52   </xs:group>
53   <xs:complexType name="studentType">
54     <xs:sequence>
55       <xs:group ref="studentGroup"></xs:group><!--引用学生基本属性-->
56       <xs:element name="computer" type="computerType"></xs:element><!--学生拥有电脑-->
57       <xs:any></xs:any><!--其他属性-->
58     </xs:sequence>
59   </xs:complexType>
60   
61   <xs:complexType name="studentsType">
62     <xs:sequence>
63       <xs:element name="student" type="studentType" minOccurs="1" maxOccurs="10">
64       </xs:element>
65     </xs:sequence>
66     <xs:attribute name="address" type="addressType"></xs:attribute><!--定义属性-->
67   </xs:complexType>
68   
69   <xs:complexType name="schoolType">
70     <xs:sequence>
71       <xs:element name="students" type="studentsType"/>
72     </xs:sequence>
73   </xs:complexType>
74   
75   <xs:element name="school" type="schoolType">  
76   </xs:element>
77 
78 </xs:schema>
View Code

 

当然针对简单的xml文件,还可以使用直接嵌套方式进行定义,但开发中不常用,这里不做叙述。

posted @ 2015-05-12 00:34  孤子  阅读(1032)  评论(0编辑  收藏  举报