Detailed introduction to DTD document type definitions in XML

Source: Internet
Author: User
Tags tutorialspoint
This article mainly introduces the Document Type Definition of DTD in XML, which is the basic knowledge in getting started with XML. For more information, see The Document Type Definition of DTD in XML, it is the basic knowledge for getting started with XML. For more information, see

XML Document Type Definition, commonly known as DTD, is a method for accurately describing the XML language. DTDs checks the validity of the vocabulary and structure of an XML document based on the syntax rules of the appropriate XML language.

The xml dtd can be specified inside a document, stored in an independent document, and linked separately.

Syntax
The basic syntax of DTD is as follows:

 

In the above syntax:

DTD usesThe element tells the parser to parse the document from the specified root element.
DTD identifier is a identifier used for Document Type Definition. it can be a path pointing to a file in the system or a URL connecting to a file on the Internet. If the DTD points to an external path, it is called an external subset. _ [] Is an optional object declaration list, called an internal subset.
Internal DTD
If an element is declared in an XML document, the DTD is called an internal DTD. To use it as an internal DTD, the standalone attribute in the XML declaration must be set to yes. This means that the declared work is independent from the external source.

Syntax

The internal DTD syntax is as follows:

 

Here, root-element is the name of the root element, and element-declarations indicates the elements we declare.

Example

The following is a simple example of an internal DTD:

         
       
       
   ]>        
 
  Tanmay Patil
       
 
  TutorialsPoint
       
 
  (011) 123-4567
   

Let's take a look at the above code:

Start declaration-use the following statement to start XML declaration:

 

DTD-followed by the XML header. _ file type declaration _ is usually called DOCTYPE:

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

The DTD Body-DOCTYPE declaration follows the DTD Body. here we declare elements, attributes, entities, and symbols:

   
   
   
 

Many elements are passed here Document vocabulary statement. Define element _ name_as "# PCDATA" type. # PCDATA indicates the parsed text data.

End declaration-finally, the declaration part of the DTD is closed using square brackets and angle brackets (]>. This is an effective end definition, followed by the XML document content.

Rules

The document type declaration must appear at the beginning of the document (only with the XML header), and cannot appear anywhere in the document.
Similar to the DOCTYPE declaration, the element declaration must start with an exclamation point.
The Name in the document type declaration must match the type of the root element.
External DTD
Elements in the external DTD are declared outside the XML document. Access by specifying the system attribute. this attribute value can be a valid. dtd file or a valid URL. To indicate that it is an external DTD, the standalone attribute of the XML declaration must be set to no. This means that the declaration contains information from external sources.

Syntax

The syntax of the external DTD is as follows:

 

Here, file-name is the. dtd extension file.

Example

The following example shows the usage of the external DTD:

           
 
  
Tanmay Patil
       
 
  
TutorialsPoint
       
 
  
(011) 123-4567
   The content of the address. DTD file is as follows:
   
   
   
 

Type

You can reference an external DTD by using a system identifier or a public identifier.

System identifier

The system identifier allows us to specify the location of the external file containing the DTD declaration. Syntax:

 

As we can see, it contains the SYSTEM keyword and a URI reference pointing to the document location.

Common Identifier

The common identifier provides a mechanism for locating DTD resources. The syntax is as follows:

 

As we can see, it starts with the PUBLIC keyword, followed by a special identifier. A public identifier is used to identify entries in a directory. Public identifiers can follow any format, but common formats are formal public identifiers (or FPIs ).

Declaration element
Declare an element in a dtd (in an xml file, if an element wants to make it legal, it must be declared in the dtd)
Syntax: And These two methods

For example:

 

Xml can be written as follows:


Notes
In dtd
All XML documents (and HTML documents) are composed of the following simple building modules:
Element
Attribute
Entity
PCDATA
CDATA
Note the following:
(1) an object is a variable used to define common text. Object reference is a reference to an object.
Most people know this HTML entity reference :"". This "no leading space" entity is used to insert an additional space in HTML in a document.
When a document is parsed by the XML parser, the object is expanded.

(2) PCDATA
PCDATA indicates the parsed character data (parsed character data ).
The character data can be considered as the text between the start tag and the end tag of the XML element.
PCDATA is the text that will be parsed by the parser. The text will be parsed into entities and tags by the parser.
Labels in text are processed as tags, and objects are expanded.
However, the parsed character data should not contain any &, <, or> Characters. you must use &, <, and> entities to replace them.
(3) CDATA
CDATA indicates character data ).
CDATA is text that will not be parsed by the parser. Tags in these texts are not treated as tags, and the objects in the tags are not expanded.

The above is a detailed description of the Document Type Definition of DTD in XML. For more information, see other related articles in the first PHP community!

Related Article

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.