DTD 介绍

A Document Type Definition defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements.
DTD(文本类型定义)定义了XML文件的合法组建群。它定义了带有合法元素列表的文件结构。

A DTD can be declared inline in your XML document, or as an external reference.
一份DTD可以在XML文件内部声明或者作为外部参数声明。


Internal DOCTYPE declaration
内部DOCTYPE声明

If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:
如果DTD是包含在XML源文件里面的,那么它应该预先包装在含有以下句法构造的DOCTYPE定义当中。

<!DOCTYPE root-element [element-declarations]>

Example XML document with a DTD: (Open it in IE5, and select view source):
含有DTD的实例XML文件:在IE5中把它打开,选择浏览源代码:

<?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>

The DTD above is interpreted like this:
上面的DTD,如下解释:

!DOCTYPE note (in line 2) defines that this is a document of the type note.
!DOCTYPE note(在第二行)定义了类型注释的文件。
!ELEMENT note (in line 3) defines the note element as having four elements: "to,from,heading,body".
!ELEMENT note(第三行)定义了"to,from,heading,body"4个元素的note元素。
!ELEMENT to (in line 4) defines the to element  to be of the type "#PCDATA".
!ELEMENT to(第4行)解释说明了成为"#PCDATA"类型之一的to元素。
!ELEMENT from (in line 5) defines the from element to be of the type "#PCDATA"
and so on.....
!ELEMENT from(第5行 )解释说明了"#PCDATA"等等类型的form元素。

External DOCTYPE declaration
外部DOCTYPE声明

If the DTD is external to your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:
如果DTD对于你的XML文件是外部而言,那么它会在含有以下句法构造的DOCTYPE声明中预先包装进去。

<!DOCTYPE root-element SYSTEM "filename">

This is the same XML document as above, but with an external DTD:  (Open it in IE5, and select view source)
如同上面的,这是份一样的 XML文件,但是带有一份外DTD:在IE5中打开,选择察看源代码。

<?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>

And this is a copy of the file "note.dtd" containing the DTD:
这是一份包含了DTD的"note.dtd"文件副本。

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>


Why use a DTD?
为什么要使用DTD?

With DTD, each of your XML files can carry a description of its own format with it.
通过DTD,你的每一个XML文件都自身携带有关它自身格式的说明。

With a DTD, independent groups of people can agree to use a common DTD for interchanging data.
通过DTD,不同群体的人们可以对使用普通的DTD来交换数据的方法达成一致。

Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
你的应用程序可以通过标准的DTD来验证你从正当外界接收到的数据是有效的。

You can also use a DTD to verify your own data.
你也可以通过DTD来检验自身的数据。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值