search
HomeBackend DevelopmentXML/RSS TutorialDetailed explanation of DTD of XML constraint technology

1.DTD official tutorial

##2.xml constraint technology:
DTD constraint: The syntax is relatively simple and the function is relatively simple.
Schema constraints appear first: the syntax is relatively complex and the functions are relatively powerful. Using a writing method similar to xml syntax, Schema constraints appear to replace DTD constraints.

3. Introduction to DTD:

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. A DTD can be declared in an XML document as a line or as an external reference.

3.1 How to import DTD:

1. Internal import:

<code>#导入方式:
<!DOCTYPE root-element [element-declarations]>
#实例:
<?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>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend</body>
</note>
</code>
(第二行)定义此文档是 note(根标签) 类型的文档。<br>
(第三行)定义 note 元素有四个元素(标签):"to、from、heading,、body"<br>
(第四行)定义 to 元素为 "#PCDATA" 类型<br>
(第五行)定义 frome 元素为 "#PCDATA" 类型<br>
(第六行)定义 heading 元素为 "#PCDATA" 类型<br>
<p>(第七行)定义 body 元素为 "#PCDATA" 类型</p>
<p>外部导入方式:<br>  本地文件:</p>
<pre class="brush:xml;"><code>#导入方式:
 <!DOCTYPE note SYSTEM "note.dtd">
#note.dtd文件内容:
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)></code>

  公共的外部导入:一般项目采用公共外部导入,比如ssh的xml文件基本上就是采用了这种方式

<code>#导入方式:
 nbsp;根元素 PUBLIC "http://rlovep.com/peace.dtd">
#如hibernate.cfg.xml:
nbsp;hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"></code>

3.2DTD语法:

  1.约束标签
 语法:

<code>   或 </code>
 类别:
空标签: EMPTY。 表示元素一定是空元素.例如::
普通字符串: (#PCDATA)。表示元素的内容一定是普通字符串(不能含有子标签)。例如:

任何内容: ANY。表示元素的内容可以是任意内容(包括子标签) 例如:

Element content:

<code>顺序问题:
<!ELEMENT 元素名称 (子元素名称 1,子元素名称 2,.....)>: 按顺序出现子标签
 
次数问题:
标签  :  必须且只出现1次。
标签+ : 至少出现1次
标签*  : 0或n次。
标签? : 0 或1次。
声明"非.../既..."类型的内容</code>

2. Constraint attributes:
Syntax:

<code><!ATTLIST 元素名称 属性名称 属性类型 默认值></code>

Attribute type:

<code>CDATA :表示普通字符串
(en1|en2|..): 表示一定是任选其中的一个值
ID:表示在一个xml文档中该属性值必须唯一。值不能以数字开头</code>

Default value:

<code>#REQUIRED 属性值是必需的
#IMPLIED   属性不是必需的
#FIXED value 属性不是必须的,但属性值是固定的</code>

3.3 The test is as follows, please read the comments carefully:

<code><?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from+,heading*,body?,(br|b))>
<!--带有子序列的元素,需要按照先后顺序出现;
    to只能出现一次
    from最少出现一次
    heading次数随意
    body出现零次或者一次
    非出现br就出现b
-->
<!--元素约束-->
<!ELEMENT to (#PCDATA)><!--pcdata元素-->
<!ELEMENT from ANY><!--任何内容的元素-->
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT br EMPTY><!--空元素-->
<!ELEMENT b EMPTY><!--空元素-->
<!--属性约束-->
<!ATTLIST to number CDATA #REQUIRED><!--必须有属性值出现,且属性值类型为字符串-->
<!ATTLIST from length CDATA "10"><!--默认属性值,不写出属性时属性值为10-->
<!--假如您不希望强制作者包含属性,并且您没有默认值选项的话,请使用关键词 #IMPLIED。-->
<!ATTLIST heading length CDATA #IMPLIED>
<!ATTLIST body length CDATA #FIXED "123"><!--属性拥有固定的值,并不允许作者改变这个值-->
<!ATTLIST br type (check|cash) "cash"><!--属性值可以为check和cash中的一个-->
]>
<note>
<to number="1234">Tove</to>
<from>Jani</from>
<heading length="10">Reminder</heading>
<body length="123">Don&#39;t forget me this weekend</body>
<br type="check"/>
</note></code>

The above is the detailed content of Detailed explanation of DTD of XML constraint technology. 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
RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

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.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment