来看看XML到底是个啥--xml约束文档

xml

概念

规定xml文档的书写规则
     * 作为框架的使用者(程序员)
       1.能够在xml中引入约束文档
       2.能够简单的读懂约束文档

逻辑图

以下是约束文档的的作用和逻辑关系的图解:
约束

分类

DTD

一种简单的约束文档

student.dtd

1
2
3
4
5
6
<!ELEMENT students (student*) >
<!ELEMENT student (name,age,sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ATTLIST student number ID #REQUIRED>

这是对于约束文档的说明:
解释

引入约束文档

1.内部dtd文档

将约束规则定义在xml文档中(不常用)
<!DOCTYPE students [
                        里面书写约束文档 
                   ]>

2.外部dtd文档

将约束文档定义在外部dtd文件中  
   1.本地的
   <!DOCTYPE 根标签 SYSTEM "外部的dtd文件">

   2.网络的
    <!DOCTYPE 根标签 PUBLIC "dtd文件的名字" "外部的dtd文件URL">

dtd实例

根据上面的约束文档书写的xml文档

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">

<students>
<student number="itcast_0001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>

</students>

Schema

一种复杂的约束技术
弥补了dtd约束文档的缺陷(dtd定义的标签里面的值不能进行限制)。

student.xsd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?xml version="1.0"?>
<!--schema文档本身就是xml文档-->

<xsd:schema xmlns="http://www.itcast.cn/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">


<!-- 定义元素,名字叫做students;元素的类型为studentsType ,自定义的studentsType需要在下面声明-->
<xsd:element name="students" type="studentsType"/>

<!-- 声明类型studentsType complex:组合类型-->
<xsd:complexType name="studentsType">
<!-- sequence表示按顺序出现 student元素 为studentType类型 最少出现0次 最多出现(未绑定)无数次-->
<xsd:sequence>
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>


<!-- 声明类型studentType complex:组合类型-->
<xsd:complexType name="studentType">
<!-- 按顺序出现里面声明的标签-->
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/> <!--字符串-->
<xsd:element name="age" type="ageType" /> <!--自定义类型-->
<xsd:element name="sex" type="sexType" /><!--自定义类型-->
</xsd:sequence>

<!-- 属性名为number,类型为自定义类型numberType,必须出现-->
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>


<!-- 声明sex类型 simple:简单类型-->
<xsd:simpleType name="sexType">
<!-- 类型为String-->
<xsd:restriction base="xsd:string">
<!-- 必须二选一-->
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>

<!-- 声明age类型 simple:简单类型-->
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<!-- 最小值为0-->
<xsd:minInclusive value="0"/>
<!-- 最大值为256-->
<xsd:maxInclusive value="256"/>
</xsd:restriction>
</xsd:simpleType>

<!-- 声明number类型 simple:简单类型-->
<xsd:simpleType name="numberType">
<!-- 类型为String-->
<xsd:restriction base="xsd:string">
<!-- 命名规则-->
<xsd:pattern value="xml_\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

引入schema文档

1.填写xml文档的根元素
2.引入xsi前缀. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
3.引入xsd文件命名空间. xsi:schemaLocation=”http://www.itcast.cn/xml student.xsd”
4.为每一个xsd约束声明一个前缀,作为标识 xmlns=”http://www.itcast.cn/xml"

实例

这是一个只有一个约束文档的实例
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.itcast.cn/xml"

xsi:schemaLocation="http://www.itcast.cn/xml student.xsd"
>
<!-- 当有多个约束文档时可加前缀 xmlns:a(前缀)-->
<student number="heima_0001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>
</students>
以下是多个约束文档的实例(spring框架的配置文件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:annotation-config />


<context:component-scan base-package="cn.cisol.mvcdemo">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>


<mvc:annotation-driven />


<mvc:resources mapping="/resources/**" location="/resources/" />



<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="htm" value="text/html" />
</map>
</property>

<property name="defaultViews">
<list>

<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
</bean>



<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>

</beans>
文章目录
  1. 1. 概念
  2. 2. 逻辑图
  3. 3. 分类
    1. 3.1. DTD
      1. 3.1.1. student.dtd
      2. 3.1.2. 引入约束文档
      3. 3.1.3. dtd实例
    2. 3.2. Schema
      1. 3.2.1. student.xsd
      2. 3.2.2. 引入schema文档
      3. 3.2.3. 实例
,