A
Document Type Definition defines the legal building blocks of an XML
document. It defines the document structure with a list of legal
elements. A DTD can be declared inline in your XML document, or as an external reference. Internal DOCTYPE declaration
|
<!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"?> |
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"?> |
And this is a copy of the file "note.dtd" containing the DTD:
这是一份包含了DTD的"note.dtd"文件副本。
<!ELEMENT note (to,from,heading,body)> |
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来验证你从正当外界接收到的数据是有效的。
The main building blocks of both XML and HTML documents are tags like <body>....</body>.
像<body>....</body>之类的标签是XML和HTML文件的主组件群。
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文件)都是由接下来的简单组件群组成的:
The following is a brief explanation of each of the building blocks:
下面是每个组件群的简短解释。
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 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 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 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 also means character data.
CDATA也是字符数据的意思。
In a DTD, XML elements are declared with a DTD element declaration.
在DTD中,XML元素是用DTDA元素的声明方式来声明的。
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> |
Empty elements are declared with the category keyword EMPTY:
空元素是用类别关键字EMPTY来声明的:
<!ELEMENT element-name EMPTY> <!ELEMENT br EMPTY> XML example: <br /> |
Elements with only character data are declared with #PCDATA inside parentheses:
纯字符数据的元素用圆括号中的#PCDATA来声明:
<!ELEMENT element-name (#PCDATA)> <!ELEMENT from (#PCDATA)> |
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 one or more children are defined with the name of the children elements inside parentheses:
……包含若干个子类别的元素用圆括号中的子元素的名字来定义。
<!ELEMENT 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 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"元素中只出现一次。
<!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"元素中出现一次或几次。
<!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或更多次数。
<!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次。
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”元素。
example: <!ELEMENT note (#PCDATA|to|from|header|message)*> |
In a DTD, Attributes are declared with an ATTLIST declaration.
在DTD中,属性是通过ATTLIST声明来声明的。
An attribute declaration has the following syntax:
属性声明的语法如下:
<!ATTLIST element-name attribute-name example: DTD example: |
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 |
IDREF |
The value is the id of another element |
IDREFS |
The value is a list of other ids |
NMTOKEN |
The value is a valid XML name |
NMTOKENS |
The value is a list of valid XML names |
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 |
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 |
DTD: Valid XML: |
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。
<!ATTLIST element-name attribute-name |
DTD: Valid XML: Valid XML: |
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关键字。
<!ATTLIST element-name attribute_name |
DTD: Valid XML: Invalid XML: |
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关键字。
<!ATTLIST element-name attribute-name |
DTD: Valid XML: Invalid XML: |
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剖析器将会提示错误。
Syntax: DTD example: |
Entities are variables used to define shortcuts to common text.
实体是用于定义普通文本快捷方式的变量。
- Entity references are references to entities.
实体参数就是反映实体特征的参数。
- Entities can be declared internal, or external
实体可以声明为内部实体或外部实体。
Syntax: <!ENTITY entity-name "entity-value"> <!ENTITY writer "Donald Duck."> XML example: <author>&writer;©right;</author> |
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;©right;</author>
Internet Explorer 5.0 can validate your XML against a DTD.
IE5.0可以通过DTD检验你的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") |
Validation can be turned off by setting the XML parser‘s validateOnParse="false".
确认可以通过XML解析器的validateOnParse="false"来关闭。
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") |
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文件)的链接
By David Moisan. Copied from his Web: http://www./
摘自网络 http://www./
<!DOCTYPE TVSCHEDULE [ <!ELEMENT TVSCHEDULE (CHANNEL+)> |
Copied from http://www./
Copied from (摘自)http://www./
<!DOCTYPE NEWSPAPER [ <!ELEMENT NEWSPAPER (ARTICLE+)> <!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED> <!ENTITY NEWSPAPER "Vervet Logic Times"> ]> |
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)>
]>
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文件。
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计划支持数据类型和命名空间。
|