Xml 简明教程

XML - DTDs

XML 文档类型声明,通常称为 DTD,是一种精确描述 XML 语言的方法。DTD 会检查 XML 文档结构的词汇和有效性,并与适当 XML 语言的语法规则对照。

The XML Document Type Declaration, commonly known as DTD, is a way to describe XML language precisely. DTDs check vocabulary and validity of the structure of XML documents against grammatical rules of appropriate XML language.

XML DTD 可以指定在文档内部,或者保留在单独的文档中,然后单独引用。

An XML DTD can be either specified inside the document, or it can be kept in a separate document and then liked separately.

Syntax

DTD 的基本语法如下−

Basic syntax of a DTD is as follows −

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

在上述语法中,

In the above syntax,

  1. The DTD starts with <!DOCTYPE delimiter.

  2. An element tells the parser to parse the document from the specified root element.

  3. DTD identifier is an identifier for the document type definition, which may be the path to a file on the system or URL to a file on the internet. If the DTD is pointing to external path, it is called External Subset.

  4. The square brackets [ ] enclose an optional list of entity declarations called Internal Subset.

Internal DTD

如果元素是在 XML 文件中声明的,则 DTD 被称为内部 DTD。要将其称为内部 DTD,XML 声明中的 standalone 属性必须设为 yes 。这意味着,声明独立于外部资源。

A DTD is referred to as an internal DTD if elements are declared within the XML files. To refer it as internal DTD, standalone attribute in XML declaration must be set to yes. This means, the declaration works independent of an external source.

Syntax

以下是内部 DTD 的语法:

Following is the syntax of internal DTD −

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

其中 root-element 是根元素的名称,element-declarations 是声明元素的位置。

where root-element is the name of root element and element-declarations is where you declare the elements.

Example

以下是内部 DTD 的一个简单示例 −

Following is a simple example of 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 us go through the above code −

Start Declaration − 使用以下声明开始 XML 声明。

Start Declaration − Begin the XML declaration with the following statement.

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

DTD − 在 XML 头之后,紧跟着文档类型声明,一般称为 DOCTYPE −

DTD − Immediately after the XML header, the document type declaration follows, commonly referred to as the DOCTYPE −

<!DOCTYPE address [

DOCTYPE 声明的元素名称开头带有一个感叹号 (!)。DOCTYPE 通知解析器此 XML 文档有一个 DTD 关联。

The DOCTYPE declaration has an exclamation mark (!) at the start of the element name. The DOCTYPE informs the parser that a DTD is associated with this XML document.

DTD Body − DOCTYPE 声明之后是 DTD 的主体,您在此声明元素、属性、实体和符号。

DTD Body − The DOCTYPE declaration is followed by body of the DTD, where you declare elements, attributes, entities, and notations.

<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone_no (#PCDATA)>

此处声明了多个元素,这些元素构成了 <name> 文档的词汇表。<!ELEMENT name (#PCDATA)>将元素 name 定义为 “#PCDATA” 类型。此处 #PCDATA 表示可解析的文本数据。

Several elements are declared here that make up the vocabulary of the <name> document. <!ELEMENT name (#PCDATA)> defines the element name to be of type "#PCDATA". Here #PCDATA means parse-able text data.

End Declaration − 最后,DTD 的声明部分使用关闭括号和关闭尖括号 ( ]> ) 关闭。这会有效结束定义,之后 XML 文档紧接着遵循。

End Declaration − Finally, the declaration section of the DTD is closed using a closing bracket and a closing angle bracket (]>). This effectively ends the definition, and thereafter, the XML document follows immediately.

Rules

  1. The document type declaration must appear at the start of the document (preceded only by the XML header) − it is not permitted anywhere else within the document.

  2. Similar to the DOCTYPE declaration, the element declarations must start with an exclamation mark.

  3. The Name in the document type declaration must match the element type of the root element.

External DTD

在外部 DTD 中,元素在 XML 文件之外声明。通过指定系统属性(可以是合法的 .dtd 文件或有效的 URL)来访问它们。为了将其作为外部 DTD 引用,必须将 XML 声明中的 standalone 属性设置为 no 。这意味着声明包括来自外部来源的信息。

In external DTD elements are declared outside the XML file. They are accessed by specifying the system attributes which may be either the legal .dtd file or a valid URL. To refer it as external DTD, standalone attribute in the XML declaration must be set as no. This means, declaration includes information from the external source.

Syntax

以下是外部 DTD 的语法−

Following is the syntax for external DTD −

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

其中 file-name 是扩展名为 .dtd 的文件。

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

Example

以下示例展示了外部 DTD 用法−

The following example shows external DTD usage −

<?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 的内容如下图所示:

The content of the DTD file address.dtd is as shown −

<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>

Types

您可以使用 system identifierspublic identifiers 来引用外部 DTD。

You can refer to an external DTD by using either system identifiers or public identifiers.

System Identifiers

系统标识符允许指定包含 DTD 声明的外部文件的位置。语法如下−

A system identifier enables you to specify the location of an external file containing DTD declarations. Syntax is as follows −

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

正如您所看到的,它包含关键词 SYSTEM 和指向文档位置的 URI 引用。

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

Public Identifiers

公共标识符提供了一种查找 DTD 资源的机制,并按如下方式编写:

Public identifiers provide a mechanism to locate DTD resources and is written as follows −

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

正如您所看到的,它以关键词 PUBLIC 开头,后跟一个专业标识符。公共标识符用于标识目录中的一个条目。公共标识符可以遵循任何格式,但是,一种常用的格式称为 Formal Public Identifiers, or FPIs

As you can see, it begins with keyword PUBLIC, followed by a specialized identifier. Public identifiers are used to identify an entry in a catalog. Public identifiers can follow any format, however, a commonly used format is called Formal Public Identifiers, or FPIs.