以下为一个表示学校的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

相关文章: