dtd(Document Type Definition):文檔定義類型,描述xml文檔的結構,它描述如下幾個方面:html
1.元素的定義規則以及元素與元素之間的關係java
2.屬性的規則數據庫
3.實體的規則mybatis
dtd與xml文檔的關係:app
類與對象框架
數據庫中的表結構與數據庫中的記錄ide
有了dtd,對於每一個xml文檔而言就有了一個自身的描述文件url
有了dtd,對不不一樣的組織的人能夠經過dtd來進行交換數據code
有了dtd,對於程序段而言,咱們能夠經過dtd來驗證一個外部的xml文檔是否有效xml
內部dtd、外部dtd、內外部結合的dtd
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student [ <!ELEMENT student (sid, sname, addr)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)> ]> <student> <sid>s01</sid> <sname>關羽</sname> <addr>南京</addr> </student>
先編寫一個demo04.dtd文件
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT student (sid, sname, addr)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)>
而後編寫一個xml文件與上面的dtd進行關聯
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student SYSTEM "demo04.dtd"> <student> <sid>s02</sid> <sname>曹操</sname> <addr></addr> </student>
改寫上面的代碼
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT student (sid, sname, addr,age)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)>
以上dtd並無定義age元素,咱們將age元素的定義放在了內部中進行描述
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student SYSTEM "demo04.dtd" [ <!ELEMENT age (#PCDATA)> ]> <student> <sid>s02</sid> <sname>曹操</sname> <addr>曹營</addr> <age>12</age> </student>
元素定義的語法:
<!ELEMENT name content>
content有如下幾種類型:
EMPTY:不能包含任何子元素和文本,可是它能夠有屬性(空元素)
ANY:能夠包含任何dtd中定義的元素
#PCDATA:只能包含字符數據,不能包含任何子元素
純元素類型:只能包含子元素,不能包含文本內容
混合類型:能夠包含子元素和文本數據
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student [ <!ELEMENT student (sid, sname, addr,hello)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT hello EMPTY> ]> <student> <sid>s01</sid> <sname>關羽</sname> <addr>南京</addr> <hello/> </student>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student [ <!ELEMENT student (sid, sname, addr,hello,world)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT hello EMPTY> <!ELEMENT world ANY> ]> <student> <sid>s01</sid> <sname>關羽</sname> <addr>南京</addr> <hello/> <world> <student> <sid></sid> <sname></sname> <addr></addr> <hello></hello> <world></world> </student> </world> </student>
對於ANY而言,在開發中不多使用。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student [ <!ELEMENT student (sid+, sname?, addr,hello,world*)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT hello EMPTY> <!ELEMENT world ANY> ]> <student> <!-- +: 1-n個 *:0-n個 ?: 0或者1個 --> <sid>s01</sid> <sid>s02</sid> <addr>南京</addr> <hello/> </student>
混合類型中能夠放字符數據也能夠放元素
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE student [ <!ELEMENT student (sid+, sname?, addr,hello,world*,helloworld)> <!ELEMENT sid (#PCDATA)> <!ELEMENT sname (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT hello EMPTY> <!ELEMENT world ANY> <!ELEMENT helloworld (#PCDATA|info|say)*> <!ELEMENT info (#PCDATA)> <!ELEMENT say (#PCDATA)> ]> <student> <!-- +: 1-n個 *:0-n個 ?: 0或者1個 --> <sid>s01</sid> <sid>s02</sid> <addr>南京</addr> <hello/> <helloworld> <info>關羽是輸出</info><say>關羽牛逼</say> </helloworld> </student>
語法:
<!ATTLIST 元素的名稱 屬性名 類型 屬性的特色>
類型:CDATA、ID、IDREF|IDREFS、枚舉類型
屬性的特色:#REQUIRED、#IMPLIED、#FIXED value、default value
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE persons[ <!ELEMENT persons (person+)> <!ELEMENT person EMPTY> <!ATTLIST person name CDATA #REQUIRED> <!ATTLIST person pid ID #REQUIRED> <!ATTLIST person ptype CDATA #REQUIRED> <!ATTLIST person parentID IDREFS #IMPLIED> <!ATTLIST person 婚姻狀態 (單身|離婚|結婚) #IMPLIED> <!ATTLIST person age CDATA "15"> <!ATTLIST person sex CDATA #FIXED "男"> ]> <persons> <person name="貂蟬" 婚姻狀態="結婚" pid="p01" ptype="mother"/> <person name="呂布" 婚姻狀態="結婚" pid="p02" ptype="father"/> <person name="呂不韋" sex="男" age="16" 婚姻狀態="單身" pid="p03" ptype="son" parentID="p02 p02"></person> </persons>
實體的定義
<!ENTITY info "這是一個普通實體">
運用時:採用&實體名;
普通實體是定義在xml文件中,應用也在xml文件中
參數實體它是定義在dtd文件中,應用在dtd文件中
<?xml version="1.0" encoding="UTF-8"?> <!ENTITY % 地址 "省份,城市,區"> <!ELEMENT person (name,age,%地址;)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT 省份 (#PCDATA)> <!ELEMENT 城市 (#PCDATA)> <!ELEMENT 區 (#PCDATA)>
xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE person SYSTEM "C:\Users\Administrator\Desktop\demo\dtd05.dtd"> <person> <name>張三</name> <age>15</age> <省份>江蘇</省份> <城市>南京</城市> <區>秦淮區</區> </person>
<?xml version="1.0" encoding="UTF-8" ?> <!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?)> <!ELEMENT databaseIdProvider (property*)> <!ATTLIST databaseIdProvider type CDATA #REQUIRED > <!ELEMENT properties (property*)> <!ATTLIST properties resource CDATA #IMPLIED url CDATA #IMPLIED > <!ELEMENT property EMPTY> <!ATTLIST property name CDATA #REQUIRED value CDATA #REQUIRED > <!ELEMENT settings (setting+)> <!ELEMENT setting EMPTY> <!ATTLIST setting name CDATA #REQUIRED value CDATA #REQUIRED > <!ELEMENT typeAliases (typeAlias*,package*)> <!ELEMENT typeAlias EMPTY> <!ATTLIST typeAlias type CDATA #REQUIRED alias CDATA #IMPLIED > <!ELEMENT typeHandlers (typeHandler*,package*)> <!ELEMENT typeHandler EMPTY> <!ATTLIST typeHandler javaType CDATA #IMPLIED jdbcType CDATA #IMPLIED handler CDATA #REQUIRED > <!ELEMENT objectFactory (property*)> <!ATTLIST objectFactory type CDATA #REQUIRED > <!ELEMENT objectWrapperFactory (property*)> <!ATTLIST objectWrapperFactory type CDATA #REQUIRED > <!ELEMENT plugins (plugin+)> <!ELEMENT plugin (property*)> <!ATTLIST plugin interceptor CDATA #REQUIRED > <!ELEMENT environments (environment+)> <!ATTLIST environments default CDATA #REQUIRED > <!ELEMENT environment (transactionManager,dataSource)> <!ATTLIST environment id CDATA #REQUIRED > <!ELEMENT transactionManager (property*)> <!ATTLIST transactionManager type CDATA #REQUIRED > <!ELEMENT dataSource (property*)> <!ATTLIST dataSource type CDATA #REQUIRED > <!ELEMENT mappers (mapper*,package*)> <!ELEMENT mapper EMPTY> <!ATTLIST mapper resource CDATA #IMPLIED url CDATA #IMPLIED class CDATA #IMPLIED > <!ELEMENT package EMPTY> <!ATTLIST package name CDATA #REQUIRED >
需求:
xml01文檔
<?xml version = "1.0" encoding="utf-8" ?> <table> <tr> <td>編號</td> <td>姓名</td> </tr> </table>
xml02文檔
<?xml version = "1.0" encoding="utf-8" ?> <table> <name>mytable</name> <width>100</width> <length>90</length> </table>
如今這2個xml文件在不一樣的文件中確定是沒有問題的,可是若是將2個xml中內容放在一塊兒就會形成命名的衝突
<?xml version = "1.0" encoding="utf-8" ?> <table> <tr> <td>編號</td> <td>姓名</td> </tr> </table> <table> <name>mytable</name> <width>100</width> <length>90</length> </table>
那麼如何解決->寫上前綴
<?xml version = "1.0" encoding="utf-8" ?> <h:table> <h:tr> <h:td>編號</h:td> <h:td>姓名</h:td> </h:tr> </h:table> <f:table> <f:name>mytable</f:name> <f:width>100</f:width> <f:length>90</f:length> </f:table>
接下來爲前綴加上命名空間約束
<?xml version = "1.0" encoding="utf-8" ?> <h:table xmlns:h="http://×××w.w3c.com/hr/html4"> <h:tr> <h:td>編號</h:td> <h:td>姓名</h:td> </h:tr> </h:table> <f:table xmlns:f="http://×××w.mytable.com/table"> <f:name>mytable</f:name> <f:width>100</f:width> <f:length>90</f:length> </f:table>
對於命名空間而言,不只能夠運用在元素上,它也能夠運用在屬性上面
命名空間就是爲了解決命名衝突的問題