首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

XML简介之DTD约束的相干实例

2012-10-16 
XML简介之DTD约束的相关实例这是一个关于Book的DTD约束文件Book.dtd!ELEMENT book (bookname,bookprice,a

XML简介之DTD约束的相关实例

这是一个关于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>


其实对于我们这些Java程序员来说,我们只要能够读懂DTD的约束文件,并且能够将简单的约束文件中的信息反映到我们的xml文件中就已经达到学习的目的了,因为,我们主要使用xml文件写配置文件的,另外,由于DTD约束存在一些弊端,所以在以后我们还可能会用到Schema约束来代替DTD约束。


热点排行