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. <br/>element is used to tell the parser to start parsing the document from the specified root element. <br/>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. <br/>Internal DTD<br/>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:
DTD Body - The DOCTYPE declaration is followed by the DTD body, where we declare elements, attributes, entities and symbols:
<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone_no (#PCDATA)>
Many elements are passed here by
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. <br/>Similar to DOCTYPE declarations, element declarations must begin with an exclamation point. <br/>The Name in the document type declaration must match the type of the root element. <br/>External DTD<br/>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. <br/>
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<br/>Declare elements in dtd (in an xml, if you want an element to be legal, you need to declare it in dtd)<br/> Syntax:< ;!ELEMENT element name category> and these two methods<br/>
For example:
<!ELEMENT br EMPTY>
The xml can be written as:
<br/>
注意点
在dtd中
所有的 XML 文档(以及 HTML 文档)均由以下简单的构建模块构成:
元素
属性
实体
PCDATA
CDATA
下面是一些注意点:
(1)实体是用来定义普通文本的变量。实体引用是对实体的引用。
大多数同学都了解这个 HTML 实体引用:" "。这个“无折行空格”实体在 HTML 中被用于在某个文档中插入一个额外的空格。
当文档被 XML 解析器解析时,实体就会被展开。
(2) PCDATA
PCDATA 的意思是被解析的字符数据(parsed character data)。
可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本。
PCDATA 是会被解析器解析的文本。这些文本将被解析器解析成实体以及标记。
文本中的标签会被当作标记来处理,而实体会被展开。
不过,被解析的字符数据不应当包含任何 &、< 或者 > 字符;需要使用 &、< 以及 > 实体来分别替换它们。
(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!