DTD Transcript

main content:

① How to describe the structure of the XML document;

② How to use DTD to define the legal elements of an XML document, and how to declare DTD inside your XML or as an external reference;

③ How to declare legal elements, attributes, entities and CDATA sections for XML documents;

④How to verify an XML document according to a DTD

 

(1 Introduction

The role of DTD (document type defined) is to define the legal building blocks of XML documents. Can be declared in-line in an XML document (internal document declaration) or as an external reference (external document declaration)

① Internal DOCTYPE declaration:

If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE declaration with the following syntax:

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

②External document declaration:

If the DTD is located outside the XML source file, it should be encapsulated in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element SYSTEM"filename">

③Function:

With DTDs, each XML document can carry a description of its own format; and independent groups can consistently use a standard DTD to exchange data.

An application can also use a standard DTD to verify data received from outside, and it can also use a DTD to verify your own data.

 

(2) XML building blocks

The main building blocks of XML and HTML documents are element tags

①XML document building block

All XML documents (and HTML documents) are composed of the following simple building blocks: elements, attributes, entities, PCDATA, CDATA

The following is an analysis of each component in turn:

1. Elements are the main building blocks of XML and HTML documents. Elements can contain text, other elements, or be empty. Examples of empty HTML elements are "hr", "br" and "img";

2. Attributes provide additional information about an element and are always placed in the start tag of an element;

3. Entities (entity references/characters) are variables used to define normal text. An entity reference is a reference to an entity that is expanded when the document is parsed by an XML parser;

4. PCDATA (parsed character data) is the text that will be parsed by the parser.

The text will be checked by the parser for entities and tags, the tags in the text will be treated as tags, and the entities will be expanded.

However, the parsed character data should not contain any &, < or > characters; they need to be replaced with &, < and > entities respectively;

5. CDATA (character data) is text that will not be parsed by the parser. The tags in these texts will not be treated as tags, and the entities in them will not be expanded.

【expand】

CDATA: The type in the attribute declaration is a string, and &, <, "" and '' are parsed with special meanings, for example: " is parsed as double quotation marks, which generally refers to text data that is not parsed by an XML parser;

PCDATA: The type in the element declaration refers to a mixed type, that is, it can contain sub-elements or strings. & and < are also parsed with special meanings. It is a term used for textual data parsed by XML parsers. Text in an XML document is usually parsed as character data, or (in terms of document type definition) called PCDATA

 

(3) DTD elements

DTD elements declare elements through element declarations

[declare an element]

Element declarations use the following syntax:

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

 1. Empty elements: declared with the category keyword EMPTY:

<!ELEMENT element-name EMPTY>
The following are examples:
<!ELEMENT br EMPTY>
XML example:
<br />

 2.只有 PCDATA 的元素:通过圆括号中的 #PCDATA 进行声明:

<!ELEMENT element-name (#PCDATA)>
以下为实例:
<!ELEMENT from (#PCDATA)>

 3.带有任何内容的元素:通过类别关键词 ANY 声明的元素,可包含任何可解析数据的组合:

<!ELEMENT element-name ANY>
以下为实例:
<!ELEMENT note ANY>

 4.带有子元素(序列)的元素:通过圆括号中的子元素名进行声明

<!ELEMENT element-name (child1,child2,...)>
以下为实例:
<!ELEMENT note (to,from,heading,body)>

    当子元素按照由逗号分隔开的序列进行声明时,这些子元素必须按照相同的顺序出现在文档中。在一个完整的声明中,子元素也必须被声明,同时子元素也可拥有子元素。"note" 元素的完整声明是:

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

 5 . 声明只出现一次的元素

<!ELEMENT element-name (child-name)>
以下为实例:
<!ELEMENT note (message)>

   上面的例子声明了:message 子元素必须出现一次,并且必须只在 "note" 元素中出现一次

 6 . 声明最少出现一次的元素

<!ELEMENT element-name (child-name+)>
以下为实例:
<!ELEMENT note (message+)>

   上面的例子中的加号(+)声明了:message 子元素必须在 "note" 元素内出现至少一次

 7 . 声明出现零次或多次的元素

<!ELEMENT element-name (child-name*)>
以下为实例:
<!ELEMENT note (message*)>

   上面的例子中的星号(*)声明了:子元素 message 可在 "note" 元素内出现零次或多次

 8 . 声明出现零次或一次的元素

<!ELEMENT element-name (child-name?)>
以下为实例:
<!ELEMENT note (message?)>

   上面的例子中的问号(?)声明了:子元素 message 可在 "note" 元素内出现零次或一次

 9 . 声明"非.../既..."类型的内容

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

   上面的例子声明了:"note" 元素必须包含 "to" 元素、"from" 元素、"header" 元素,以及非 "message" 元素既 "body" 元素

 10 . 声明混合型的内容

<!ELEMENT note (#PCDATA|to|from|header|message)*>

   上面的例子声明了:"note" 元素可包含出现零次或多次的 PCDATA、"to"、"from"、"header" 或者 "message"

 

(4)DTD属性

在 DTD 中,属性通过 ATTLIST 声明来进行声明

属性声明语法:

<!ATTLIST element-name元素名 attribute-name属性名 attribute-type属性类型 attribute-value>

DTD 实例:

<!ATTLIST payment type CDATA "check">

XML 实例:

<payment type="check" />

 

(5)XML元素vs属性

在HTML重多使用属性,但在XML中,使用子元素,会感觉更像数据信息

同时要尽量避免使用属性:

一些属性具有以下问题:

属性不能包含多个值(子元素可以);不容易扩展(为以后需求的变化);无法描述结构(子元素可以);更难以操纵程序代码;不容易测试,针对DTD

上面有具体解析

 

(6)DTD实体

实体是用于定义引用普通文本或特殊字符的快捷方式的变量

实体引用是对实体的引用,可在内部或外部进行声明

内部实体声明:

<!ENTITY entity-name实体名称 "entity-value">
以下为实例
DTD 实例:
<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright runoob.com">
XML 实例:
<author>&writer;&copyright;</author>

   注意: 一个实体由三部分构成: 一个和号 (&), 一个实体名称, 以及一个分号 (;)

外部实体声明

<!ENTITY entity-name SYSTEM "URI/URL">
以下为实例
DTD 实例:
<!ENTITY writer SYSTEM "http://www.runoob.com/entities.dtd">
<!ENTITY copyright SYSTEM "http://www.runoob.com/entities.dtd">
XML 例子:
<author>&writer;&copyright;</author>

 

(7)DTD验证

使用IE可根据某个 DTD 来验证您的 XML

【通过 XML 解析器进行验证】

当试图打开某个 XML 文档时,XML 解析器有可能会产生错误。通过访问 parseError 对象,就可以取回引起错误的确切代码、文本甚至所在的行

注意: load() 方法用于文件,而 loadXML() 方法用于字符串

【关闭验证:通过把 XML 解析器的 validateOnParse 设置为 "false",就可以关闭验证】

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326196023&siteId=291194637
DTD