分享

DTD教程

 jinzq 2007-04-03

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来检验自身的数据。



DTD - XML 建立 blocks(二)
/ 2006-09-21

DTD 介绍 DTD - 元素

The main building blocks of both XML and HTML documents are tags like <body>....</body>.
像<body>....</body>之类的标签是XML和HTML文件的主组件群。


The building blocks of XML documents
XML文件的组件群

Seen from a DTD point of view, all XML documents (and HTML documents) are made up by the following simple building blocks:
从DTD来看,所有的XML文件(和HTML文件)都是由接下来的简单组件群组成的:

  • Elements
    元素
  • Attributes
    属性
  • Entities
    实体
  • PCDATA
    被解析的字符数据(Parsed Character Data)
  • CDATA
    字符数据值(Character Data)

The following is a brief explanation of each of the building blocks:
下面是每个组件群的简短解释。


Elements
元素

Elements are the main building blocks of both XML and HTML documents.
元素是XML和HTML文件的主组件群。

Examples of HTML elements are "body" and "table". Examples of XML elements could be "note" and "message". Elements can contain text, other elements, or be empty. Examples of empty HTML elements are "hr", "br" and "img".
HTML元素的实例是"body" 和"table"。XML元素实例是"note" 和"message"。元素可以包括文本、其它元素或者空值。空值html元素的实例是"hr", "br" 和"img".

Examples:
例如:

<body>body text in between</body>
<message>some message in between</message>


Attributes
属性

Attributes provide extra information about elements.
属性提供关于元素的额外信息。

Attributes are always placed inside the starting tag of an element. Attributes always come in name/value pairs. The following "img" element has additional information about a source file:
属性总是置于元素的开始标签里面。属性一般是以“名称(name)/值(value)”这样的形势一对对的出现。下面的“img”元素就是关于源文件的补充信息:

<img src="computer.gif" />

The name of the element is "img". The name of the attribute is "src". The value of the attribute is "computer.gif". Since the element itself is empty it is closed by a " /".
元素名称是“img”。属性名称“src”。属性值是"computer.gif"。因为元素自身是空值,所以它是以“/”结束的。


Entities
实体

Entities are variables used to define common text. Entity references are references to entities.
实体是对于定义普通文件的变量。实体参数是定义实体的参数。

Most of you will know the HTML entity reference: " ". This "no-breaking-space" entity is used in HTML to insert an extra space in a document. Entities are expanded when a document is parsed by an XML parser.
很多人都知道HTML实体参数:" "。这里的"no-breaking-space"实体是在HTML中用来在文本中插入一个额外空间的。当XML剖析器解析文件时,实体就会得到扩展。

The following entities are predefined in XML:
下面的实体是在XML中预定的:

Entity References
实体参数
Character
字符
< <
> >
& &
" "
'
 

PCDATA

PCDATA means parsed character data.
PCDATA的意思是被解析的字符数据。

Think of character data as the text found between the start tag and the end tag of an XML element.
把字符数据当作XML元素的开始标签与结束标签之间的文本。

PCDATA is text that will be parsed by a parser. Tags inside the text will be treated as markup and entities will be expanded. 
剖析器会分析PCDATA文本。文本中的标签会被当作标示的字体,实体将会扩展。


CDATA

CDATA also means character data.
CDATA也是字符数据的意思。

CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.
剖析器不会解析CDATA文本。文本中的标签不会作为标示字体,实体也将不会得到扩展。



DTD - 元素(三)
/ 2006-09-21

DTD - XML 建立 blocks DTD - 属性

In a DTD, XML elements are declared with a DTD element declaration.
在DTD中,XML元素是用DTDA元素的声明方式来声明的。


Declaring an Element
声明元素

In the DTD, XML elements are declared with an element declaration. An element declaration has the following syntax:
在DTD中,XML元素是用XML元素的声明方式来声明的。元素的声明方式含有以下句法构造:

<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>


Empty elements
空元素

Empty elements are declared with the category keyword EMPTY:
空元素是用类别关键字EMPTY来声明的:

<!ELEMENT element-name EMPTY>

example:
<!ELEMENT br EMPTY>
XML example:
<br />


Elements with only character data
纯字符数据的元素

Elements with only character data are declared with #PCDATA inside parentheses:
纯字符数据的元素用圆括号中的#PCDATA来声明:

<!ELEMENT element-name (#PCDATA)>

example:
<!ELEMENT from (#PCDATA)>


Elements with any contents
以any内容声明的元素

Elements declared with the category keyword ANY, can contain any combination of parsable data:
以类别关键字ANY声明的元素能包括任何部分数据的结合体。

<!ELEMENT element-name ANY>
example:
<!ELEMENT note ANY>


Elements with children (sequences)
以child关键字声明的元素(按次序排列)

Elements with one or more children are defined with the name of the children elements inside parentheses:
……包含若干个子类别的元素用圆括号中的子元素的名字来定义。

<!ELEMENT element-name 
(child-element-name)>
or
<!ELEMENT element-name
(child-element-name,child-element-name,.....)>
example:
<!ELEMENT note (to,from,heading,body)>

When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children. The full declaration of the "note" element will be:
当子元素被逗号依次隔开声明时,子元素必须在文件中以相同的顺序出现。在一个完整的声明中,子元素必须也被声明,同时子元素也能有子元素。"note"元素的完整声明是:

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


Declaring only one occurrence of the same element 
给相同的元素声明一个发生事件

<!ELEMENT element-name (child-name)>
example:
<!ELEMENT note (message)>

The example declaration above declares that the child element message must occur once, and only once inside the "note" element.
上面的实例声明了子元素信息必须出现一次,并且在"note"元素中只出现一次。


Declaring minimum one occurrence of the same element
声明相同元素的最小发生事件

<!ELEMENT element-name (child-name+)>
example:
<!ELEMENT note (message+)>

The + sign in the example above declares that the child element message must occur one or more times inside the "note" element.
上面实例中的加号声明了子元素信息必须在"note"元素中出现一次或几次。


Declaring zero or more occurrences of the same element 
声明0或更多相同元素的出现事件

<!ELEMENT element-name (child-name*)>
example:
<!ELEMENT note (message*)>

The * sign in the example above declares that the child element message can occur zero or more times inside the "note" element.
在上面实例中的*号声明了子元素信息可以在"note"元素中出现0或更多次数。


Declaring zero or one occurrences of the same element 
声明0或1次相同元素的发生事件

<!ELEMENT element-name (child-name?)>
example:
<!ELEMENT note (message?)>

The ? sign in the example above declares that the child element message can occur zero or one times inside the "note" element.
在上面的实例中的?号声明了子元素信息可以在"note"元素中出现0或1次。


Declaring either/or content
声明either/or内容

example:
<!ELEMENT note (to,from,header,(message|body))>

The example above declares that the "note" element must contain a "to" element, a "from" element, a "header" element, and either a "message" or a "body" element.
上面的事例声明了"note"元素必须包含一个”TO”元素、一个“form”元素、一个“header”元素以及“message”或“body”元素。


Declaring mixed content
声明混合内容

example:
<!ELEMENT note (#PCDATA|to|from|header|message)*>
The example above declares that the "note" element can contain zero or more occurrences of parsed character, "to", "from", "header", or "message" elements.
上面的实例声明了"note"元素可以包含0或更多分列字符(“or”、“form”、“header”或“message”元素)。




DTD - 属性(四)
/ 2006-09-21

DTD - 元素 DTD - 实体

In a DTD, Attributes are declared with an ATTLIST declaration.
在DTD中,属性是通过ATTLIST声明来声明的。


Declaring Attributes
声明属性

An attribute declaration has the following syntax:
属性声明的语法如下:

<!ATTLIST element-name attribute-name 
attribute-type default-value>
example:
DTD example:
<!ATTLIST payment type CDATA "check">

XML example:
<payment type="check" />

The attribute-type can have the following values:
属性类型含有以下值:

Value
Explanation
解释

CDATA

The value is character data
字符数据值

(en1|en2|..)

The value must be one from an enumerated list
必须是来自列表中的一个值

ID

The value is a unique id
唯一独立的id值

IDREF

The value is the id of another element
其它元素的id值

IDREFS

The value is a list of other ids
其它id的列表值

NMTOKEN

The value is a valid XML name
其值为一个有效的XML名称

NMTOKENS

The value is a list of valid XML names
其值为一组有效的XML名称列表值

ENTITY

The value is an entity
其值为一个实体

ENTITIES

The value is a list of entities
其值为一组实体的列表

NOTATION

The value is a name of a notation
其值为一个符号的名称

xml:

The value is a predefined xml value
预定的XML值

The default-value can have the following values:
省略补充含有以下值:

Value
Explanation
解释

value

The default value of the attribute
属性默认值

#REQUIRED

The attribute value must be included in the element
包含于元素内的属性值

#IMPLIED

The attribute does not have to be included
可包含也可不包含于元素内

#FIXED value

The attribute value is fixed
固定的属性值



Specifying a Default attribute value
指定一个默认的属性值

DTD:
<!ELEMENT square EMPTY>

<!ATTLIST square width CDATA "0">
Valid XML:
<square width="100" />

In the example above, the "square" element is defined to be an empty element with a "width" attribute of  type CDATA. If no width is specified, it has a default value of 0.
在上面的例子中,"square"元素定义为含有CDATA类型的"width"属性的空元素。如果没有指定width值,那它默认为0。


#IMPLIED

Syntax
语法

<!ATTLIST element-name attribute-name 
attribute-type #IMPLIED>

Example
例子

DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax="555-667788" />
Valid XML:
<contact />

Use the #IMPLIED keyword if you don‘t want to force the author to include an attribute, and you don‘t have an option for a default value.
如果你不想让author元素包含一个属性值或对默认值不具备选择权,那么你不妨使用#IMPLIED关键字。


#REQUIRED

Syntax
语法

<!ATTLIST element-name attribute_name 
attribute-type #REQUIRED>

Example
例子

DTD:
<!ATTLIST person number CDATA #REQUIRED>
Valid XML:
<person number="5677" />
Invalid XML:

<person />

Use the #REQUIRED keyword if you don‘t have an option for a default value, but still want to force the attribute to be present.
如果你对默认值不具备选择权却想却有想让属性值存在,那么你不妨使用#REQUIRED关键字。


#FIXED

Syntax
语法

<!ATTLIST element-name attribute-name 
attribute-type #FIXED "value">

Example
例子

DTD:
<!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML:
<sender company="Microsoft" />
Invalid XML:
<sender company="W3Schools" />

Use the #FIXED keyword when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error.
当你希望属性是一个固定值且不希望让author元素去改变它,我们建议你使用#FIXED关键字。如果author包含另外一个值,XML剖析器将会提示错误。


Enumerated attribute values
列举属性值方法Enumerated attribute values:

Syntax:
<!ATTLIST element-name
attribute-name (en1|en2|..) default-value>
DTD example:
<!ATTLIST payment type (check|cash) "cash">

XML example:
<payment type="check" />
or
<payment type="cash" />
Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.
当你希望得到一个固定合法的属性值时,请使用列举属性值(Enumerated attribute values)的方法。


DTD - 实体(五)
/ 2006-09-21

DTD - 属性 DTD 校验

Entities are variables used to define shortcuts to common text.
实体是用于定义普通文本快捷方式的变量。

- Entity references are references to entities.
实体参数就是反映实体特征的参数。
- Entities can be declared internal, or external
实体可以声明为内部实体或外部实体。


Internal Entity Declaration
内部实体声明

Syntax: 
<!ENTITY entity-name "entity-value">

DTD Example:
<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">
XML example:
<author>&writer;&copyright;</author>
 

External Entity Declaration
外部实体声明

Syntax: 
<!ENTITY entity-name SYSTEM "URI/URL">

DTD Example:
<!ENTITY writer    
SYSTEM "http://www./dtd/entities.dtd">
<!ENTITY copyright
SYSTEM "http://www./dtd/entities.dtd">
XML example:
<author>&writer;&copyright;</author>


DTD 校验(六)
/ 2006-09-21

DTD - 实体 DTD - 来自网络的举例

Internet Explorer 5.0 can validate your XML against a DTD.
IE5.0可以通过DTD检验你的XML。


Validating with the XML Parser
用XML剖析器确认

If you try to open an XML document, the XML Parser might generate an error. By accessing the parseError object, the exact error code, the error text, and even the line that caused the error can be retrieved:
如果你想打开一个XML文件,那XML剖析器会产生错误。通过访问分析错误目标,精确的错误代码、错误文本以及引起错误的线路都能找到。

Note: The load( ) method is used for files, while the loadXML( ) method is used for strings.
注意:The load( ) method是用于文件的,而the loadXML( ) method是用于字符串的。

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="true"

xmlDoc.load("note_dtd_error.xml")

document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)

你可以自己尝试一下 或 查看这个XML文件


Turning Validation off
关闭确认

Validation can be turned off by setting the XML parser‘s validateOnParse="false".
确认可以通过XML解析器的validateOnParse="false"来关闭。

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="false"

xmlDoc.load("note_dtd_error.xml")

document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)

自己尝试一下吧


A general XML Validator
XML通用的确认方法

To help you validate your xml files, we have created this link so that you can Validate any XML file.
如想确认你的XML文件,我们创建了一个可以快速访问Validate any XML file(确认XML文件)的链接


The parseError Object
分析错误对象(Object)

You can read more about the parseError object in our XML DOM tutorial.
如想了解更多关于parseError 对象的情况,请访问我们DOM tutorial (DOM教程)。



DTD - 来自网络的举例(七)
/ 2006-09-21

DTD 校验 DTD 摘要

TV Schedule DTD
DTD电视节目

By David Moisan. Copied from his Web: http://www./
摘自网络 http://www./

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

]>
 

Newspaper Article DTD
DTD报纸文摘

Copied from http://www./ 
Copied from (摘自)http://www./

<!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">
]>
 

Product Catalog DTD
DTD产品目录

Copied from http://www./ 
Copied from (摘自)http://www./

<!DOCTYPE CATALOG [
<!ENTITY AUTHOR "John Doe">

<!ENTITY COMPANY "JD Power Tools, Inc.">
<!ENTITY EMAIL "jd@jd-tools.com">

<!ELEMENT CATALOG (PRODUCT+)>

<!ELEMENT PRODUCT
(SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)>
<!ATTLIST PRODUCT
NAME CDATA #IMPLIED
CATEGORY (HandTool|Table|Shop-Professional) "HandTool"
PARTNUM CDATA #IMPLIED
PLANT (Pittsburgh|Milwaukee|Chicago) "Chicago"

INVENTORY (InStock|Backordered|Discontinued) "InStock">

<!ELEMENT SPECIFICATIONS (#PCDATA)>
<!ATTLIST SPECIFICATIONS
WEIGHT CDATA #IMPLIED
POWER CDATA #IMPLIED>

<!ELEMENT OPTIONS (#PCDATA)>
<!ATTLIST OPTIONS
FINISH (Metal|Polished|Matte) "Matte"
ADAPTER (Included|Optional|NotApplicable) "Included"

CASE (HardShell|Soft|NotApplicable) "HardShell">

<!ELEMENT PRICE (#PCDATA)>
<!ATTLIST PRICE
MSRP CDATA #IMPLIED
WHOLESALE CDATA #IMPLIED
STREET CDATA #IMPLIED
SHIPPING CDATA #IMPLIED>

<!ELEMENT NOTES (#PCDATA)>

]>
 

DTD 摘要(八)
/ 2006-09-21

DTD - 来自网络的举例

DTD Summary
DTD概要

This tutorial has taught you how to describe the structure of an XML document.
这份教程是说明XML文件结构的

You have learned how to use a DTD to define the legal elements of an XML document, and how the DTD can be declared inside your XML document, or as an external reference.
你已经知道了如何使用DTD来定义XML文件中的合法元素,以及如何将DTD从你的XML文件中或将其作为外部参数来声明。

You have learned how to declare the legal elements, attributes, entities, and CDATA sections for XML documents.
你已经知道了如何来声明合法元素,属性,实体以及XML文件的CDATA部分。

You have also seen how to validate an XML document against a DTD.
你也知道了如何通过DTD来验证XML文件。


Now You Know DTD, What‘s Next?
现在你知道了DTD,那接下来该如何做呢?

The next step is to learn about XML Schema.
下一步是了解XML计划。

XML Schema is used to define the legal elements of an XML document, just like a DTD. We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs.
XML计划是用于定义XML文件中像DTD的合法元素的。我们来想一想不久XML计划将会用于更多诸如代替DTD的网络应用软件。

XML Schema is an XML-based alternative to DTD.
XML 计划对于DTD来说是一种以XML为基础的可供选择的办法。

Unlike DTD, XML Schemas has support for data types and namespaces.
不像DTD,XML计划支持数据类型和命名空间。

If you want to learn more about XML Schema, please visit our XML Schema tutorial.
如果你想了解更多XML计划,请访问我们的XML Schema tutorial. (XML计划教程)。

网址:http://www.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多