DTD explanation of XML-XML file constraints

Source: Internet
Author: User
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:

  • XML DTD

  • XML Schema

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:

        
          

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.