Detailed 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'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>#导入方式: <!DOCTYPE 根元素 PUBLIC "http://rlovep.com/peace.dtd"> #如hibernate.cfg.xml: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"></code>
3.2DTD语法:
1.约束标签
语法:
<code> <!ELEMENT 元素名称 类别> 或 <!ELEMENT 元素名称 (元素内容)></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'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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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

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

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,

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 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 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

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach
