DTD即文档类型定义,是一种XML约束模式语言,是XML文件的验证机制,属于XML文件组成的一部分。
一个 DTD文档包含:元素的定义规则,元素间关系的定义规则,元素可以使用的属性,可以使用的实体或符号规则。web
DTD和XSD相比:DTD 是使用非 XML 语法编写的。
DTD 不可扩展,不支持命名空间,只提供很是有限的数据类型 .
DTD 如今基本已被XSD文档取代,可是,仍有个别在使用 好比 Mybatis mapper xml文件spring
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!DOCTYPE 根元素 SYSTEM/PUBLIC "DTD文件路径"> SYSTEM表示本地 PUBLC 表示网络
上面用的是公共DTD,DTD名称格式为"注册//组织//类型 标签//语言"
"注册" 指示组织是否由国际标准化组织(ISO)注册,+表示是,-表示不是;
"组织" 即组织名称,如:mybatis.org;
"类型" 通常是DTD;
"标签" 是指定公开文本描述,即对所引用的公开文本的惟一描述性名称,后面可附带版本号,如Mapper 3.0。
"语言" EN指英语;
http://mybatis.org/dtd/mybatis-3-mapper.dtd 表示外部DTD文件URIspring-mvc
DTD基本语法:<!ELEMENT NAME CONTENT>网络
其中: mybatis
- ELEMENT是关键字,是不能修改的
- NAME表示元素名称
- CONTENT是元素类型,必需要大写!
<!ELEMENT 班级 (学生+)> <!ELEMENT 学生 (名字,年龄,介绍)> <!ELEMENT 名字 (#PCDATA)> 一个.dtd文件 <!ELEMENT 年龄 (#PCDATA)> <!ELEMENT 介绍 (#PCDATA)>
使用DTD验证模式须要在XML文件的头部声明mvc
<?xml version="1.0" encoding="utf-8"?> <!-- 引入dtd文件,约束这个xml --> <!DOCTYPE 班级 SYSTEM "myclass.dtd"> <班级> <学生> <名字>小红</名字> <年龄>10</年龄> <介绍>学习刻苦</介绍> </学生> </班级>
XML Schema语言也就是XSD。XML Schema描述了XML文档的结构。检查该XML文档是否符合其要求。app
XSD是DTD替代者的缘由,一是据未来的条件可扩展,二是比DTD丰富和有用,三是用XML书写,四是支持数据类型,五是支持命名空间。ide
<?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:p="http://www.springframework.org/schema/p" 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">
首先说明最基本的头部命名空间信息,配置文件必须的部分,固定部分 xmlns="http://www.springframework.org/schema/beans" 声明xml文件默认的命名空间,表示未使用其余命名空间的全部标签的默认命名空间。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明XML Schema 实例,声明后就能够使用 schemaLocation属性了。
xmlns:context="http://www.springframework.org/schema/context" 这是spring配置文件里面须要使用到context的标签,声明前缀为context的命名空间,在容器启动的时候找到对应的命名空间处理器处理。当命名空间被定义在元素的开始标签中时,全部带有相同前缀的子元素都会与同一个命名空间相关联。
xsi:schemaLaction部分: http://www.springframework.org/schema/beans --- 表示区别命名空间url http://www.springframework.org/schema/beans/spring-beans.xsd --- 表示命名空间对应xsd文件获取地址(也只是一个映射地址)。学习
在spring-webmvc.jar包中找到META_INF/spring.handlersurl
http\://www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler
META_INF/spring.schemas
http\://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd http\://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd http\://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd http\://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd http\://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd