dtd文件通过定义可以使用的 元素、属性 以及它们的关系,规定了xml的结构。
dtd可以定义在xml内部,也可以定义成独立的 .dtd 文件,以被xml引用。
1)xml内部定义dtd
该dtd仅对所在的xml文件使用
<?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note>
2)独立的 .dtd 文件定义
该dtd文件,被其它xml引用
独立的 note.dtd 文件
<!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
note.xml 引用了 note.dtd
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
3)dtd中的注释
dtd中使用与 html相同的注释,即 <!-- -->