Table of Contents
" >The above DTD is explained as follows:
" >External document declaration
" >Why use DTD?
" >XML document building module
" > Elements
" >Attributes
" >entity
下面的实体在 XML 中被预定义:" >下面的实体在 XML 中被预定义:
PCDATA" >PCDATA
声明一个元素" >声明一个元素
空元素" >空元素
例子:" >例子:
XML例子:" >XML例子:
只有 PCDATA 的元素" >只有 PCDATA 的元素
带有任何内容的元素" >带有任何内容的元素
带有子元素(序列)的元素" >带有子元素(序列)的元素
声明只出现一次的元素" >声明只出现一次的元素
声明最少出现一次的元素" >声明最少出现一次的元素
声明出现零次或多次的元素" >声明出现零次或多次的元素
声明出现零次或一次的元素" >声明出现零次或一次的元素
声明“非.../既...”类型的内容" >声明“非.../既...”类型的内容
声明混合型的内容" >声明混合型的内容
声明属性" >声明属性
DTD 实例:" >DTD 实例:
XML 实例:" >XML 实例:
以下是属性类型的选项:" >以下是属性类型的选项:
默认值参数可使用下列值:" >默认值参数可使用下列值:
规定一个默认的属性值" >规定一个默认的属性值
DTD:" >DTD:
合法的 XML:" >合法的 XML:
#IMPLIED" >#IMPLIED
语法:" >语法:
#REQUIRED" >#REQUIRED
#FIXED" >#FIXED
列举属性值" >列举属性值
DTD 例子:" >DTD 例子:
XML 例子:" >XML 例子:
一个内部实体声明" >一个内部实体声明
一个外部实体声明" >一个外部实体声明
通过 XML 解析器进行验证" >通过 XML 解析器进行验证
关闭验证" >关闭验证
通用的 XML 验证器" >通用的 XML 验证器
parseError 对象" >parseError 对象
电视节目表 DTD" >电视节目表 DTD
报纸文章 DTD " >报纸文章 DTD
产品目录 DTD" >产品目录 DTD
Home Backend Development XML/RSS Tutorial Crazy XML study notes (3) -----------XML and DTD

Crazy XML study notes (3) -----------XML and DTD

Feb 21, 2017 pm 02:14 PM

Document type definition (DTD) can define legal XML document building blocks. It uses a series of legal elements to define the structure of the document.

DTD can be declared in an XML document as a line or as an external reference.

##Internal DOCTYPE declaration

If the DTD is included in your XML source file, it should pass the following syntax Wrapped in a DOCTYPE declaration:

<!DOCTYPE 根元素 [元素声明]>
Copy after login

XML document instance with DTD

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>George</to>
  <from>John</from>
  <heading>Reminder</heading>
  <body>Don&#39;t forget the meeting!</body>
</note>
Copy after login

The above DTD is explained as follows:

!DOCTYPE note (second line) defines this document as a note type document.

!ELEMENT note (Third line) Definition note The element has four elements: "to, from, heading,, body"

!ELEMENT to (Fourth line) defines the to element as "#PCDATA" type

!ELEMENT from (fifth line) defines the frome element as "#PCDATA" type

!ELEMENT heading (Sixth line) Definition heading The element is "#PCDATA" type

!ELEMENT body (Seventh line) Definitionbody The element is "#PCDATA" type

External document declaration

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

<!DOCTYPE 根元素 SYSTEM "文件名">
Copy after login

This XML document is the same as the XML document above, but Have an external DTD: (And select the "View Source" command.)

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting!</body>
</note>
Copy after login

This is the "note.dtd" file containing the DTD:

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

Why use DTD?

#With a DTD, each of your XML files can carry a description of its own format.

With DTD, independent groups can consistently use a standard DTD to exchange data.

Your application can also use a standard DTD to verify data received from the outside.

You can also use DTDs to validate your own data.

XML document building module

All XML documents (as well as HTML documents) are composed of the following simple building blocks:

  • Elements

  • Attributes

  • Entity

  • PCDATA

  • CDATA

The following is a brief description of each building block.

Elements

Elements are the main building blocks of XML and HTML documents .

Examples of HTML elements are "body" and "table". Examples of XML elements are "note" and "message" . Elements can contain text, other elements, or be empty. Examples of empty HTML elements are "hr", "br", and "img".

Instance:
<body>body text in between</body>
<message>some message in between</message>
Copy after login

Attributes

Attributes provide additional information about the element.

Attributes are always placed in the opening tag of an element. Properties always appear in name/value pairs. The following "img" element holds additional information about the source file:

<img src="computer.gif" />
Copy after login

The name of the element is "img". The name of the attribute is "src". The value of the property is "computer.gif". Since the element itself is empty, it is closed with a " /".

entity

实体是用来定义普通文本的变量。实体引用是对实体的引用。

大多数同学都了解这个 HTML 实体引用:" "。这个“无折行空格”实体在 HTML 中被用于在某个文档中插入一个额外的空格。

当文档被 XML 解析器解析时,实体就会被展开。

下面的实体在 XML 中被预定义:

实体引用字符
<<
>>
&&
""
''

PCDATA

PCDATA 的意思是被解析的字符数据(parsed character data)。

可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本。

PCDATA 是会被解析器解析的文本。这些文本将被解析器检查实体以及标记。

文本中的标签会被当作标记来处理,而实体会被展开。

不过,被解析的字符数据不应当包含任何 &、< 或者 > 字符;需要使用 &、< 以及 > 实体来分别替换它们。


在一个 DTD 中,元素通过元素声明来进行声明。

声明一个元素

在 DTD 中,XML 元素通过元素声明来进行声明。元素声明使用下面的语法:

<!ELEMENT 元素名称 类别>
Copy after login

或者

<!ELEMENT 元素名称 (元素内容)>
Copy after login

空元素

空元素通过类别关键词EMPTY进行声明:

<!ELEMENT 元素名称 EMPTY>
Copy after login

例子:

<!ELEMENT br EMPTY>
Copy after login

XML例子:

<br />
Copy after login

只有 PCDATA 的元素

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

<!ELEMENT 元素名称 (#PCDATA)>
Copy after login

例子:

<!ELEMENT from (#PCDATA)>
Copy after login

带有任何内容的元素

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

<!ELEMENT 元素名称 ANY>
Copy after login

例子:

<!ELEMENT note ANY>
Copy after login

带有子元素(序列)的元素

带有一个或多个子元素的元素通过圆括号中的子元素名进行声明:

<!ELEMENT 元素名称 (子元素名称 1)>
Copy after login

或者

<!ELEMENT 元素名称 (子元素名称 1,子元素名称 2,.....)>
Copy after login

例子:

<!ELEMENT note (to,from,heading,body)>
Copy after login

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

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



Copy after login

声明只出现一次的元素

<!ELEMENT 元素名称 (子元素名称)>
Copy after login

例子:

<!ELEMENT note (message)>
Copy after login

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

声明最少出现一次的元素

<!ELEMENT 元素名称 (子元素名称+)>
Copy after login

例子:

<!ELEMENT note (message+)>
Copy after login

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

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

<!ELEMENT 元素名称 (子元素名称*)>
Copy after login

例子:

<!ELEMENT note (message*)>
Copy after login

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

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

<!ELEMENT 元素名称 (子元素名称?)>
Copy after login

例子:

<!ELEMENT note (message?)>
Copy after login

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

声明“非.../既...”类型的内容

例子:

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

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

声明混合型的内容

例子:

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

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

声明属性

属性声明拥使用下列语法:

<!ATTLIST 元素名称 属性名称 属性类型 默认值>
Copy after login

DTD 实例:

<!ATTLIST payment type CDATA "check">
Copy after login

XML 实例:

<payment type="check" />
Copy after login
Copy after login

以下是属性类型的选项:

类型描述
CDATA值为字符数据 (character data)
(en1|en2|..)此值是枚举列表中的一个值
ID值为唯一的 id
IDREF值为另外一个元素的 id
IDREFS值为其他 id 的列表
NMTOKEN值为合法的 XML 名称
NMTOKENS值为合法的 XML 名称的列表
ENTITY值是一个实体
ENTITIES值是一个实体列表
NOTATION此值是符号的名称
xml:值是一个预定义的 XML 值

默认值参数可使用下列值:

解释
属性的默认值
#REQUIRED属性值是必需的
#IMPLIED属性不是必需的
#FIXED value属性值是固定的

规定一个默认的属性值

DTD:

<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
Copy after login

合法的 XML:

<square width="100" />
Copy after login

在上面的例子中,"square" 被定义为带有 CDATA 类型的 "width" 属性的空元素。如果宽度没有被设定,其默认值为0 。

#IMPLIED

语法

<!ATTLIST 元素名称 属性名称 属性类型 #IMPLIED>
Copy after login

例子

DTD:

<!ATTLIST contact fax CDATA #IMPLIED>
Copy after login

合法的 XML:

<contact fax="555-667788" />
Copy after login

合法的 XML:

<contact />
Copy after login

假如您不希望强制作者包含属性,并且您没有默认值选项的话,请使用关键词 #IMPLIED。

#REQUIRED

语法

<!ATTLIST 元素名称 属性名称 属性类型 #REQUIRED>
Copy after login

例子

DTD:

<!ATTLIST person number CDATA #REQUIRED>
Copy after login

合法的 XML:

<person number="5677" />
Copy after login

非法的 XML:

<person />
Copy after login

假如您没有默认值选项,但是仍然希望强制作者提交属性的话,请使用关键词 #REQUIRED。

#FIXED

语法

<!ATTLIST 元素名称 属性名称 属性类型 #FIXED "value">
Copy after login

例子

DTD:

<!ATTLIST sender company CDATA #FIXED "Microsoft">
Copy after login

合法的 XML:

<sender company="Microsoft" />
Copy after login

非法的 XML:

<sender company="W3School" />
Copy after login

如果您希望属性拥有固定的值,并不允许作者改变这个值,请使用 #FIXED 关键词。如果作者使用了不同的值,XML 解析器会返回错误。

列举属性值

语法:

<!ATTLIST 元素名称 属性名称 (en1|en2|..) 默认值>
Copy after login

DTD 例子:

<!ATTLIST payment type (check|cash) "cash">
Copy after login

XML 例子:

<payment type="check" />
Copy after login
Copy after login

或者

<payment type="cash" />
Copy after login

如果您希望属性值为一系列固定的合法值之一,请使用列举属性值。


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

实体引用是对实体的引用。

实体可在内部或外部进行声明。

一个内部实体声明

语法:

<!ENTITY 实体名称 "实体的值">
Copy after login

例子:

DTD 例子:

<!ENTITY writer "Bill Gates">
<!ENTITY copyright "Copyright W3School.com.cn">
Copy after login

XML 例子:

<author>&writer;&copyright;</author>
Copy after login
Copy after login

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

一个外部实体声明

语法:

<!ENTITY 实体名称 SYSTEM "URI/URL">
Copy after login

例子:

DTD 例子:

<!ENTITY writer SYSTEM "http://www.w3school.com.cn/dtd/entities.dtd">
<!ENTITY copyright SYSTEM "http://www.w3school.com.cn/dtd/entities.dtd">
Copy after login

XML 例子:

<author>&writer;&copyright;</author>
Copy after login
Copy after login

通过 XML 解析器进行验证

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

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

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="true"
xmlDoc.load("note_dtd_error.xml")

document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)
Copy after login

关闭验证

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

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="false"
xmlDoc.load("note_dtd_error.xml")

document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)
Copy after login

Try it Yourself

通用的 XML 验证器

为了帮助您验证 XML 文件,我们创建了此链接,这样你就可以验证任何 XML 文件了。

parseError 对象

电视节目表 DTD

<!DOCTYPE TVSCHEDULE [

<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER,DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY (DATE,(HOLIDAY|PROGRAMSLOT+)+)>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME,TITLE,DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)> 
<!ELEMENT DESCRIPTION (#PCDATA)>

<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>
<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>

]>
Copy after login

报纸文章 DTD

<!DOCTYPE NEWSPAPER [ 

<!ELEMENT NEWSPAPER (ARTICLE+)>
<!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)>
<!ELEMENT HEADLINE (#PCDATA)>
<!ELEMENT BYLINE (#PCDATA)>
<!ELEMENT LEAD (#PCDATA)>
<!ELEMENT BODY (#PCDATA)>
<!ELEMENT NOTES (#PCDATA)> 

<!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>
<!ATTLIST ARTICLE EDITOR CDATA #IMPLIED>
<!ATTLIST ARTICLE DATE CDATA #IMPLIED>
<!ATTLIST ARTICLE EDITION CDATA #IMPLIED>

<!ENTITY NEWSPAPER "Vervet Logic Times">
<!ENTITY PUBLISHER "Vervet Logic Press">
<!ENTITY COPYRIGHT "Copyright 1998 Vervet Logic Press">

]>
Copy after login

产品目录 DTD

拷贝自:http://www.php.cn/

<!DOCTYPE CATALOG [

<!ENTITY AUTHOR "John Doe">
<!ENTITY COMPANY "JD Power Tools, Inc.">
<!ENTITY EMAIL "jd@jd-tools.com">

<!ELEMENT CATALOG (PRODUCT+)>

<!ELEMENT PRODUCT
(SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)>
<!ATTLIST PRODUCT
NAME CDATA #IMPLIED
CATEGORY (HandTool|Table|Shop-Professional) "HandTool"
PARTNUM CDATA #IMPLIED
PLANT (Pittsburgh|Milwaukee|Chicago) "Chicago"
INVENTORY (InStock|Backordered|Discontinued) "InStock">

<!ELEMENT SPECIFICATIONS (#PCDATA)>
<!ATTLIST SPECIFICATIONS
WEIGHT CDATA #IMPLIED
POWER CDATA #IMPLIED>

<!ELEMENT OPTIONS (#PCDATA)>
<!ATTLIST OPTIONS
FINISH (Metal|Polished|Matte) "Matte" 
ADAPTER (Included|Optional|NotApplicable) "Included"
CASE (HardShell|Soft|NotApplicable) "HardShell">

<!ELEMENT PRICE (#PCDATA)>
<!ATTLIST PRICE
MSRP CDATA #IMPLIED
WHOLESALE CDATA #IMPLIED
STREET CDATA #IMPLIED
SHIPPING CDATA #IMPLIED>

<!ELEMENT NOTES (#PCDATA)>

]>
Copy after login

 

以上就是疯狂XML学习笔记(3)-----------XML与DTD 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

 

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles