Home  >  Article  >  Backend Development  >  Detailed introduction to DTD document type definition in XML

Detailed introduction to DTD document type definition in XML

黄舟
黄舟Original
2017-03-10 19:32:352043browse

This article mainly introduces the DTD document type definition in XML, which is the basic knowledge for introductory learning of XML. Friends who need it can refer to

XML document type definition, commonly known as DTD, it is a Accurately describe the way the XML language is used. DTDs check the validity of the vocabulary and structure of an XML document against the syntax rules of the appropriate XML language.

XML DTD can be specified inside the document, or it can be saved in a separate document and linked separately.

Syntax
The basic syntax of DTD is as follows:

<!DOCTYPE element DTD identifier   
[   
    declaration1   
    declaration2   
    ........   
]>

In the above syntax:

DTD starts with < ;!DOCTYPE delimiter starts.
element is used to tell the parser to start parsing the document from the specified root element.
DTD identifier is an identifier used for document type definition. It can be a path to a file in the system or a URL connected to a file on the Internet. If the DTD points to an external path, it is called an external subset. Inside _[] is an optional list of entity declarations, called the inner subset.
Internal DTD
If an element is declared inside an XML document then the DTD is called an internal DTD. In order for this to be used as an internal DTD, the standalone attribute in the XML declaration must be set to yes. This means that the claimed work is independent of external sources.

Syntax

The internal DTD syntax is as follows:

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

Here root-element is the name of the root element, element -declarations represents the elements we declare.

Example

Here is a simple example of an internal DTD:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>  
<!DOCTYPE address [   
<!ELEMENT address (name,company,phone)>  
    <!ELEMENT name (#PCDATA)>  
    <!ELEMENT company (#PCDATA)>  
    <!ELEMENT phone (#PCDATA)>  
]>  
<address>  
    <name>Tanmay Patil</name>  
    <company>TutorialsPoint</company>  
    <phone>(011) 123-4567</phone>  
</address>

Let’s take a look at the above code:

Start Declaration - Begin the XML declaration with the following statement:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

DTD - Immediately after the XML header, the _Document Type Declaration_ is as follows, often referred to as DOCTYPE:

8f9446ec6ebae9443291fcb085f11483 defines element_name_ as "#PCDATA" type. Here #PCDATA means parsable text data.

End declaration - Finally, the declaration part of the DTD is closed using square brackets and angle brackets (]>). This is a valid closing definition, followed immediately by the XML document content.

Rules

The document type declaration must appear at the beginning of the document (only with the XML header) and is not allowed to appear anywhere else in the document.
Similar to DOCTYPE declarations, element declarations must begin with an exclamation point.
The Name in the document type declaration must match the type of the root element.
External DTD
In an external DTD elements are declared outside the XML document. Accessed by specifying the system attribute, which can be a valid .dtd file or a valid URL. To indicate that it is an external DTD, the XML declaration's standalone attribute must be set to no. This means that the statement contains information from an external source.

Syntax

The following is the syntax of external DTD:

<!DOCTYPE root-element SYSTEM "file-name">

Here file-name is the file with .dtd extension.

Example

The following example shows the use of an external DTD:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>  
<!DOCTYPE address SYSTEM "address.dtd">  
<address>  
    <name>Tanmay Patil</name>  
    <company>TutorialsPoint</company>  
    <phone>(011) 123-4567</phone>  
</address>  
DTD 文件 address.dtd 的内容如下所示:   
  
<!ELEMENT address (name,company,phone)>  
<!ELEMENT name (#PCDATA)>  
<!ELEMENT company (#PCDATA)>  
<!ELEMENT phone (#PCDATA)>

Type

We can reference an external DTD by using the system identifier or the public identifier.

System Identifier

The system identifier allows us to specify the location of an external file that contains the DTD declaration. The syntax is as follows:

<!DOCTYPE name SYSTEM "address.dtd" [...]>

As we can see, it contains the SYSTEM keyword and a URI reference pointing to the location of the document.

Public Identifier

The public identifier provides a mechanism for locating DTD resources. It is written as follows:

<!DOCTYPE name PUBLIC "-//Beginning XML//DTD Address Example//EN">

As we can see, it starts with the PUBLIC keyword, followed by the specialized identifier. Public identifiers are used to identify entries in the directory. Public identifiers can follow any format, but a commonly used format is Formal Public Identifiers (or FPIs).

Declare elements
Declare elements in dtd (in an xml, if you want an element to be legal, you need to declare it in dtd)
Syntax:< ;!ELEMENT element name category> and 9520cecfc7a58a6caa1ba87e324100e7 these two methods
2ee155e0c7e067ab1cbd2b0f22c6e69e

For example:

<!ELEMENT br EMPTY>

The xml can be written as:

<br/>

注意点
在dtd中
所有的 XML 文档(以及 HTML 文档)均由以下简单的构建模块构成:
元素
属性
实体
PCDATA
CDATA
下面是一些注意点:
(1)实体是用来定义普通文本的变量。实体引用是对实体的引用。
大多数同学都了解这个 HTML 实体引用:" "。这个“无折行空格”实体在 HTML 中被用于在某个文档中插入一个额外的空格。
当文档被 XML 解析器解析时,实体就会被展开。
Detailed introduction to DTD document type definition in XML

(2) PCDATA
PCDATA 的意思是被解析的字符数据(parsed character data)。
可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本。
PCDATA 是会被解析器解析的文本。这些文本将被解析器解析成实体以及标记。
文本中的标签会被当作标记来处理,而实体会被展开。
不过,被解析的字符数据不应当包含任何 &、d9cc50603bc7f01c48909aac2ab47bd6 字符;需要使用 &、< 以及 > 实体来分别替换它们。
(3)CDATA
CDATA 的意思是字符数据(character data)。
CDATA 是不会被解析器解析的文本。在这些文本中的标签不会被当作标记来对待,其中的实体也不会被展开。

The above is the detailed content of Detailed introduction to DTD document type definition in XML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn