search
HomeBackend DevelopmentXML/RSS TutorialDetailed introduction to DTD document type definition in XML

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:


The DOCTYPE declaration contains an exclamation point (!) at the beginning of the element name. DOCTYPE tells the parser that there is a DTD associated with this document.

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 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 these two methods

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 是会被解析器解析的文本。这些文本将被解析器解析成实体以及标记。
文本中的标签会被当作标记来处理,而实体会被展开。
不过,被解析的字符数据不应当包含任何 &、 字符;需要使用 &、df6ed20ae940f563c21bfd880540d8df 实体来分别替换它们。
(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
Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

Is There an RSS Alternative Based on JSON?Is There an RSS Alternative Based on JSON?Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment