这是一个关于Book的DTD约束文件
Book.dtd
<!ELEMENT book (bookname,bookprice,author)>
<!ELEMENT bookname (#PCDATA)><!ELEMENT bookprice (#PCDATA)>
<!ELEMENT author (#PCDATA)>
在外部引用该约束的文件
Book.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE bookshelf SYSTEM "Book.dtd">
<book>
<bookname>java程序设计</bookname>
<bookprice>30元</bookprice>
<author>lijizh1013</author>
</book>
再来看一个将约束写到xml文件中的电视时间表的实例:
<?xml version="1.0" encoding="UTF-8"?>
//约束声明部分
<!DOCTYPE TVSCHEDULE [
<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER,DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY (DATE,(HOLIDAY|PROGRAMSLOT+)+)>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME,TITLE,DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>
<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>
<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>
]>
//正式的xml文件部分
<TVSCHEDULE NAME = "LIJIZH">
<CHANNEL CHAN = "1013">
<BANNER>A</BANNER>
<DAY>
<DATE>1991-10-13</DATE>
<HOLIDAY>100 DAY</HOLIDAY>
</DAY>
</CHANNEL>
</TVSCHEDULE>
其他相关实例:
一个新闻报纸的实例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE NEWSPAPER [
<!ELEMENT NEWSPAPER (ARTICLE+)>
<!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)>
<!ELEMENT HEADLINE (#PCDATA)>
<!ELEMENT BYLINE (#PCDATA)>
<!ELEMENT LEAD (#PCDATA)>
<!ELEMENT BODY (#PCDATA)>
<!ELEMENT NOTES (#PCDATA)>
<!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>
<!ATTLIST ARTICLE EDITOR CDATA #IMPLIED>
<!ATTLIST ARTICLE DATE CDATA #IMPLIED>
<!ATTLIST ARTICLE EDITION CDATA #IMPLIED>
<!ENTITY NEWSPAPER "Vervet Logic Times">
<!ENTITY PUBLISHER "Vervet Logic Press">
<!ENTITY COPYRIGHT "Copyright 1998 Vervet Logic Press">
]>
<NEWSPAPER>
<ARTICLE AUTHOR = "lijizh">
<HEADLINE>STUDYING</HEADLINE>
<BYLINE>1990-10-12</BYLINE>
<LEAD>lijizh</LEAD>
<BODY>This is a pare of newspaper!</BODY>
<NOTES>Please look at the newspaper!</NOTES>
</ARTICLE>
</NEWSPAPER>
一个关于国家从中央到地方的最高长官的名称的实例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE country [
<!ELEMENT country (countries+)>
<!ELEMENT countries (province+)>
<!ELEMENT province (city+)>
<!ELEMENT city (countryside+)>
<!ATTLIST countries name CDATA #REQUIRED>
<!ATTLIST province
name CDATA #REQUIRED
stadholder CDATA #REQUIRED>
<!ATTLIST city
name CDATA #REQUIRED
mayor CDATA #REQUIRED>
<!ATTLIST countryside
name CDATA #REQUIRED
alcalde CDATA #REQUIRED>
]>
<country>
<countries name = "China">
<province name ="HeBeiSheng" stadholder = "张三">
<city name = "BaoDingShi" mayor = "李四">
<countryside name ="BaiGouZhen" alcalde = "王五">
lijizh
</countryside>
</city>
</province>
<province name ="HeNanSheng" stadholder = "马六">
<city name = "KaiFengShi" mayor = "橫七">
<countryside name = "GaoYangZhen" alcalde = "竖八">
lijizh
</countryside>
</city>
</province>
</countries>
</country>