XML DTDIntroduction
XML DTD (Document Type Definition, documentationclass型定义) is a用于定义XMLdocumentationstructure and 规则 language. throughDTD, 我们可以指定XMLdocumentationin允许using 元素, property, 实体以及它们之间 relationships, from 而implementation for XMLdocumentation has 效性verification.
DTD 作用
- 定义XMLdocumentation structure, including允许 元素 and 它们 嵌套relationships
- 定义元素可以package含 property and property 取值规则
- 定义可以 in XMLdocumentationinusing 实体
- implementationXMLdocumentation has 效性verification, 确保documentation符合预期 structure and 规则
- improvingXMLdocumentation 互operation性 and reliability
DTD and 格式良 good XML
一个格式良 good XMLdocumentation只需要遵循basic XML语法规则, 而一个 has 效 XMLdocumentation不仅要格式良 good , 还必须符合其 for 应 DTD or Schema定义 规则.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE 根元素 SYSTEM "DTDfilepath">
<根元素>
<子元素> in 容</子元素>
<另一个元素 property="值"> in 容</另一个元素>
</根元素>
DTD class型
根据DTD 位置 and 引用方式, DTD可以分 for 两种class型: in 部DTD and out 部DTD.
in 部DTD
in 部DTD is 直接package含 in XMLdocumentation in 部 DTD定义, using<!DOCTYPE>声明, 并将DTD in 容放 in 方括号[] in .
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, year, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book id CDATA #REQUIRED>
]>
<library>
<book id="B001">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
</book>
</library>
out 部DTD
out 部DTD is store in 单独filein DTD定义, XMLdocumentationthrough引用该file来usingDTD. out 部DTD可以分 for 两种:
- systemDTD (SYSTEM) : 由本地system or specific组织定义 DTD, using
SYSTEM关键字引用 - 公共DTD (PUBLIC) : 由国际组织 or 行业协会定义 公共DTD, using
PUBLIC关键字引用, 并package含一个公共标识符
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<book id="B001">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
</book>
</library>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTMLdocumentation</title>
</head>
<body>
<h1>欢迎usingXHTML</h1>
</body>
</html>
混合DTD
混合DTD is 同时package含 in 部DTD and out 部DTD 组合, in 部DTD可以scale or 覆盖 out 部DTD 定义.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd" [
<!ELEMENT publisher (#PCDATA)>
]>
<library>
<book id="B001">
<title>XMLBasicstutorial</title>
<author>张三</author>
<publisher>科技出版社</publisher>
<year>2025</year>
<price>99.00</price>
</book>
</library>
DTD元素声明
in DTDin, 元素声明用于定义XMLdocumentationin允许using 元素, 元素 in 容class型以及元素之间 嵌套relationships.
元素声明 语法
<!ELEMENT 元素名 in 容class型>
元素 in 容class型
DTDin元素 in 容class型可以分 for 以 under 几种:
1. 空元素 (EMPTY)
表示元素不能package含任何 in 容, 只能 has property.
<!ELEMENT br EMPTY>
2. 纯文本元素 (#PCDATA)
表示元素只能package含文本 in 容, 不能package含子元素.
<!ELEMENT title (#PCDATA)>
3. 混合 in 容元素
表示元素可以同时package含文本 and 子元素.
<!ELEMENT paragraph (#PCDATA | b | i | u)*>
4. 子元素元素
表示元素只能package含指定 子元素.
<!ELEMENT book (title, author, year, price)>
元素出现次数限定符
in 声明子元素时, 可以using限定符来指定子元素出现 次数:
| 限定符 | 含义 | example |
|---|---|---|
无 |
必须出现且仅出现一次 | (title) |
? |
可选, 最 many 出现一次 | (title?) |
* |
可选, 可出现任意次数 | (title*) |
+ |
必须出现, 可出现任意次数 | (title+) |
元素顺序
in 声明子元素时, 元素 顺序很 important , XMLdocumentation必须按照DTDin声明 顺序排列子元素.
<!ELEMENT book (title, author, year, price)>
DTDproperty声明
in DTDin, property声明用于定义元素可以package含 property, property class型以及property 默认值.
property声明 语法
<!ATTLIST 元素名 property名 propertyclass型 默认值>
propertyclass型
DTDinproperty class型可以分 for 以 under 几种:
1. CDATA
表示property值可以 is 任意字符data.
2. 枚举class型 (ENUMERATED)
表示property值必须 from 指定 枚举listin选择.
3. ID
表示property值必须 is 唯一 标识符, 用于标识documentationin 元素.
4. IDREF
表示property值必须 is documentationin另一个元素 IDproperty值, 用于建立元素之间 引用relationships.
5. IDREFS
表示property值可以package含 many 个IDREF值, 用空格分隔.
6. ENTITY
表示property值必须 is 已声明 实体名称.
7. ENTITIES
表示property值可以package含 many 个实体名称, 用空格分隔.
8. NMTOKEN
表示property值必须 is has 效 XML名称标记.
9. NMTOKENS
表示property值可以package含 many 个NMTOKEN值, 用空格分隔.
property默认值
in 声明property时, 可以指定以 under 几种默认值class型:
| 默认值class型 | 含义 | example |
|---|---|---|
#REQUIRED |
property is 必需 , 必须 in 元素in出现 | id CDATA #REQUIRED |
#IMPLIED |
property is 可选 , 默认值由application程序决定 | class CDATA #IMPLIED |
#FIXED "值" |
property is 可选 , 但such as果出现, 其值必须 is 指定 固定值 | version CDATA #FIXED "1.0" |
默认值 |
property is 可选 , such as果不出现, 将using指定 默认值 | lang CDATA "zh-CN" |
<!ATTLIST book
id ID #REQUIRED
category CDATA #IMPLIED
language CDATA "zh-CN"
format (paperback|hardcover|ebook) "paperback"
>
DTD实体声明
in DTDin, 实体声明用于定义可以 in XMLdocumentationin重复using 文本片段 or out 部resource.
实体 class型
DTDin 实体可以分 for 以 under 几种:
1. in 部common实体
in 部common实体 is in DTD in 部定义 实体, using&实体名; in XMLdocumentationin引用.
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, publisher, year, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ENTITY publisher "科技出版社">
]>
<library>
<book>
<title>XMLBasicstutorial</title>
<author>张三</author>
<publisher>&publisher;</publisher>
<year>2025</year>
<price>99.00</price>
</book>
</library>
2. out 部common实体
out 部common实体 is in DTD out 部定义 实体, usingSYSTEM关键字引用 out 部resource.
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, description)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ENTITY bookdesc SYSTEM "book_description.txt">
]>
<library>
<book>
<title>XMLBasicstutorial</title>
<author>张三</author>
<description>&bookdesc;</description>
</book>
</library>
3. parameter实体
parameter实体 is 只能 in DTD in 部using 实体, using%实体名;引用.
<!DOCTYPE library [
<!ENTITY % bookelements "title, author, year, price">
<!ELEMENT library (book+)>
<!ELEMENT book (%bookelements;)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
实践case: creation并usingDTD
casedescribes
creation一个XML DTDfile, 用于定义graph书information structure, 然 after using该DTDverificationXMLdocumentation.
implementation步骤
- creation一个名 for
books.dtdDTDfile - in DTDfilein定义graph书information structure
- creation一个名 for
books.xmlXMLfile - in XMLfilein引用DTD
- usingxmllinttoolverificationXMLdocumentation
最终code
<!ELEMENT books (book+)>
<!ELEMENT book (title, author, year, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book
id ID #REQUIRED
category CDATA #IMPLIED
language CDATA "zh-CN"
>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books SYSTEM "books.dtd">
<books>
<book id="B001" category="计算机">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
</book>
<book id="B002" category="计算机" language="en">
<title>advanced XML programming</title>
<author>John Doe</author>
<year>2024</year>
<price>129.00</price>
</book>
</books>
verification过程
usingxmllintverificationXMLdocumentation:
# usingDTDverificationXMLdocumentation xmllint --valid --noout books.xml # such as果没 has 输出, 说明documentation is has 效
互动练习
练习1: 识别DTD声明error
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, year, price)
<!ELEMENT title PCDATA>
<!ELEMENT author (#PCDATA)>
<!ATTLIST book id ID #REQUIRED category CDATA #IMPLIED
]>
DTD声明存 in 以 under error:
- book元素声明缺 few 结束尖括号
> - title元素声明in#PCDATA缺 few 括号
- ATTLIST声明缺 few 结束尖括号
>
练习2: creationDTD and XMLdocumentation
<!ELEMENT students (student+)>
<!ELEMENT student (name, age, gender, major)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT major (#PCDATA)>
<!ATTLIST student
id ID #REQUIRED
>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students SYSTEM "students.dtd">
<students>
<student id="S001">
<name>张三</name>
<age>20</age>
<gender>男</gender>
<major>计算机科学</major>
</student>
<student id="S002">
<name>李四</name>
<age>21</age>
<gender>女</gender>
<major>软件工程</major>
</student>
</students>