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的實(shí)例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文件是外部而言,那么它會在含有以下句法構(gòu)造的DOCTYPE聲明中預(yù)先包裝進(jìn)去。
<!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,你的每一個(gè)XML文件都自身攜帶有關(guān)它自身格式的說明。
With a DTD, independent groups of people can agree to use a common DTD for interchanging data.
通過DTD,不同群體的人們可以對使用普通的DTD來交換數(shù)據(jù)的方法達(dá)成一致。
Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
你的應(yīng)用程序可以通過標(biāo)準(zhǔn)的DTD來驗(yàn)證你從正當(dāng)外界接收到的數(shù)據(jù)是有效的。
The main building blocks of both XML and HTML documents are tags like <body>....</body>.
像<body>....</body>之類的標(biāo)簽是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:
下面是每個(gè)組件群的簡短解釋。
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元素的實(shí)例是"body" 和"table"。XML元素實(shí)例是"note" 和"message"。元素可以包括文本、其它元素或者空值??罩礹tml元素的實(shí)例是"hr", "br" 和"img".
Examples:
例如:
<body>body text in between</body> <message>some message in between</message> |
Attributes provide extra information about elements.
屬性提供關(guān)于元素的額外信息。
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:
屬性總是置于元素的開始標(biāo)簽里面。屬性一般是以“名稱(name)/值(value)”這樣的形勢一對對的出現(xiàn)。下面的“img”元素就是關(guān)于源文件的補(bǔ)充信息:
<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"。因?yàn)樵刈陨硎强罩?,所以它是以?”結(jié)束的。
Entities are variables used to define common text. Entity references are references to entities.
實(shí)體是對于定義普通文件的變量。實(shí)體參數(shù)是定義實(shí)體的參數(shù)。
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實(shí)體參數(shù):" "。這里的"no-breaking-space"實(shí)體是在HTML中用來在文本中插入一個(gè)額外空間的。當(dāng)XML剖析器解析文件時(shí),實(shí)體就會得到擴(kuò)展。
The following entities are predefined in XML:
下面的實(shí)體是在XML中預(yù)定的:
Entity References 實(shí)體參數(shù) |
Character 字符 |
---|---|
< | < |
> | > |
& | & |
" | " |
' | ‘ |
PCDATA means parsed character data.
PCDATA的意思是被解析的字符數(shù)據(jù)。
Think of character data as the text found between the start tag and the end tag of an XML element.
把字符數(shù)據(jù)當(dāng)作XML元素的開始標(biāo)簽與結(jié)束標(biāo)簽之間的文本。
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文本。文本中的標(biāo)簽會被當(dāng)作標(biāo)示的字體,實(shí)體將會擴(kuò)展。
CDATA also means character data.
CDATA也是字符數(shù)據(jù)的意思。
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元素的聲明方式來聲明的。元素的聲明方式含有以下句法構(gòu)造:
<!ELEMENT element-name category> |
Empty elements are declared with the category keyword EMPTY:
空元素是用類別關(guān)鍵字EMPTY來聲明的:
<!ELEMENT element-name EMPTY> <!ELEMENT br EMPTY> XML example: <br /> |
Elements with only character data are declared with #PCDATA inside parentheses:
純字符數(shù)據(jù)的元素用圓括號中的#PCDATA來聲明:
<!ELEMENT element-name (#PCDATA)> <!ELEMENT from (#PCDATA)> |
Elements declared with the category keyword ANY, can contain any combination of parsable data:
以類別關(guān)鍵字ANY聲明的元素能包括任何部分?jǐn)?shù)據(jù)的結(jié)合體。
<!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:
……包含若干個(gè)子類別的元素用圓括號中的子元素的名字來定義。
<!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:
當(dāng)子元素被逗號依次隔開聲明時(shí),子元素必須在文件中以相同的順序出現(xiàn)。在一個(gè)完整的聲明中,子元素必須也被聲明,同時(shí)子元素也能有子元素。"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.
上面的實(shí)例聲明了子元素信息必須出現(xiàn)一次,并且在"note"元素中只出現(xiàn)一次。
<!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.
上面實(shí)例中的加號聲明了子元素信息必須在"note"元素中出現(xiàn)一次或幾次。
<!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.
在上面實(shí)例中的*號聲明了子元素信息可以在"note"元素中出現(xiàn)0或更多次數(shù)。
<!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.
在上面的實(shí)例中的?號聲明了子元素信息可以在"note"元素中出現(xiàn)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"元素必須包含一個(gè)”TO”元素、一個(gè)“form”元素、一個(gè)“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:
省略補(bǔ)充含有以下值:
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值,那它默認(rèn)為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元素包含一個(gè)屬性值或?qū)δJ(rèn)值不具備選擇權(quán),那么你不妨使用#IMPLIED關(guān)鍵字。
<!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.
如果你對默認(rèn)值不具備選擇權(quán)卻想?yún)s有想讓屬性值存在,那么你不妨使用#REQUIRED關(guān)鍵字。
<!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.
當(dāng)你希望屬性是一個(gè)固定值且不希望讓author元素去改變它,我們建議你使用#FIXED關(guān)鍵字。如果author包含另外一個(gè)值,XML剖析器將會提示錯(cuò)誤。
Syntax: DTD example: |
Entities are variables used to define shortcuts to common text.
實(shí)體是用于定義普通文本快捷方式的變量。
- Entity references are references to entities.
實(shí)體參數(shù)就是反映實(shí)體特征的參數(shù)。
- Entities can be declared internal, or external
實(shí)體可以聲明為內(nèi)部實(shí)體或外部實(shí)體。
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檢驗(yàn)?zāi)愕腦ML。
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:
如果你想打開一個(gè)XML文件,那XML剖析器會產(chǎn)生錯(cuò)誤。通過訪問分析錯(cuò)誤目標(biāo),精確的錯(cuò)誤代碼、錯(cuò)誤文本以及引起錯(cuò)誤的線路都能找到。
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".
確認(rèn)可以通過XML解析器的validateOnParse="false"來關(guān)閉。
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.
如想確認(rèn)你的XML文件,我們創(chuàng)建了一個(gè)可以快速訪問Validate any XML file(確認(rèn)XML文件)的鏈接
By David Moisan. Copied from his Web: http://www./
摘自網(wǎng)絡(luò) 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文件結(jié)構(gòu)的
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.
你已經(jīng)知道了如何使用DTD來定義XML文件中的合法元素,以及如何將DTD從你的XML文件中或?qū)⑵渥鳛橥獠繀?shù)來聲明。
You have learned how to declare the legal elements, attributes, entities, and CDATA sections for XML documents.
你已經(jīng)知道了如何來聲明合法元素,屬性,實(shí)體以及XML文件的CDATA部分。
You have also seen how to validate an XML document against a DTD.
你也知道了如何通過DTD來驗(yàn)證XML文件。
The next step is to learn about XML Schema.
下一步是了解XML計(jì)劃。
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計(jì)劃是用于定義XML文件中像DTD的合法元素的。我們來想一想不久XML計(jì)劃將會用于更多諸如代替DTD的網(wǎng)絡(luò)應(yīng)用軟件。
XML Schema is an XML-based alternative to DTD.
XML 計(jì)劃對于DTD來說是一種以XML為基礎(chǔ)的可供選擇的辦法。
Unlike DTD, XML Schemas has support for data types and namespaces.
不像DTD,XML計(jì)劃支持?jǐn)?shù)據(jù)類型和命名空間。
|