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