We write a document to constrain the writing specification of an XML document, which is called an XML constraint.
1. Brief introduction to XML file constraints and DTD
We write a document to constrain the writing specification of an XML document, which is called an XML constraint.
Common constraints include:
Basic concepts of DTD:
Document type definition
DTD files are generally used in combination with XML files, mainly to constrain XML files.
The XML file is introduced into the DTD file, so that the XML can customize tags, but is subject to the constraints of the DTD file. For example, if we use XML to describe the information of a class in the previous section <面积>
Labels and syntax are correct, but they do not conform to the semantics. how can students describe using area? At this time, we need to use the DTD file to constrain this XML.
Yang Guo
Male
20
<面积>
100
1.1 DTD constraints Quick Start case
Basic syntax:
We also take the class as an example to compile the following DTD file, myClass. dtd:
The first line indicates that the root element is a class, and the student child element is one or more.
The second line indicates that the child element of the student is the name, age, and description.
There are no child elements under the name, so # PCDATA indicates that any text can be put in the name.
The age and description are similar.
Compile the myClass. xml file and introduce the DTD file as follows:
<班级>
<学生>
<名字>
Zhou Xiaoxing
<年龄>
23
<介绍>
Hard work
<学生>
<名字>
Lin Xiao
<年龄>
25
<介绍>
Be a good student
SYSTEM, which indicates that the current DTD file is local.
If the description is PUBLIC, it indicates that the introduced DTD file comes from the network.
The introduced DTD file does not work at this time. if we add child elements to the student element <面积>
Open the XML file, and the browser still does not report an error.
<班级>
<学生>
<名字>
Zhou Xiaoxing
<年龄>
23
<介绍>
Hard work
<面积>
100 square meters
<学生>
<名字>
Lin Xiao
<年龄>
25
<介绍>
Be a good student
We needVerify the correctness of XML documents by programming.
IE5 and later browsers have built-in XML parsing tools: Microsoft. XMLDOM. developers can write JavaScript code, use this parsing tool to load XML files, and perform DTD verification on XML files.
We compile myxmltools.html to verify this XML, as follows: