<!ATTLIST 元素名称 属性名称 数据类型 附加条件>
TIP:声明元素的时候,元素的数据类型是PCDATA
属性的时候,实行的数据类型是CDATA
dtd的代码:
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT movies (movie+)> <!ELEMENT movie (title,actor,rating)> <!ELEMENT title (#PCDATA)> <!ELEMENT actor (#PCDATA)> <!ELEMENT rating (#PCDATA)> <!ATTLIST movie id CDATA #required> <!ATTLIST movie type CDATA #IMPLIED>
对应xml代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE movies SYSTEM "demo9.dtd"> <movies> <movie id="1" type=""> <title/> <actor/> <rating/> </movie> <movie id="1"> <title/> <actor/> <rating/> </movie> </movies>
含义 |
语法 |
|
必填属性 |
||
#IMPLIED |
属性可有可无 |
|
default |
默认值 |
|
FIXED |
固定值 |
|
枚举 |
枚举 |
|
ID |
唯一 |
例题:写出下面xml的dtd文件
dtd代码:
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT books (book+)> <!ELEMENT book (#PCDATA)> <!ATTLIST book id ID #required> <!ATTLIST book medium CDATA #FIXED '纸质'> <!ATTLIST book type CDATA '计算机'> <!ATTLIST book pub (北京大学出版社|工业出版社|邮电出版社) "北京大学出版社"> <!ATTLIST book pagesize CDATA #IMPLIED>
对应的xml代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE books SYSTEM "demo10.dtd"> <books> <book id="s001" medium="纸质" type="计算机" pub="北京大学出版社" pagesize="300"> </book> </books>