Author DTD _ Baidu Encyclopedia: http://baike.baidu.com/view/147436.htm
DTD
Document Type Definition
(Document Type Definition)
DTD is a set of syntax rules for tags. It is part of the xml1.0 specification and is a validation mechanism for XML files. It is part of an XML file.
DTD is an effective method to ensure that the XML document format is correct. You can compare the XML document and DTD file to see whether the document complies with the specifications, and whether the elements and labels are correctly used. A dtd contains the definition rules of elements, the definition rules of the relationship between elements, the available attributes of elements, and the entity or symbol rules that can be used.
The XML file provides an application data exchange format. The DTD makes the XML file the standard for data exchange, because different companies only need to define the standard DTD, companies can establish and verify XML files according to the DTD, so that they can easily establish standards and exchange data, meeting network sharing and data interaction.
A dtd is an ASCII text file with the suffix. DTD.
========================================================== =====================================/
1. About DTD
Document Type Definition (DTD) can define valid XML document building modules. It uses a series of legal elements to define the structure of a document.
DTD can be declared in XML documents in a line or used as an external reference.
Internal doctype Declaration
If a DTD is contained in your xml source file, it should be packaged in a doctype declaration using the following syntax:
**************************************** *************************************
<! Doctype root element [element Declaration]>
**************************************** *************************************
XML document instance with DTD
**************************************** *************************************
<? 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 <Body> don't forget me this weekend </body>
</Note>
**************************************** *************************************
The above DTD is explained as follows:
! Doctype note (Row 2) defines this document as a note-type document.
! Element note (Row 3) defines four elements of note: "to, from, heading, and body"
! Element to (Row 4) defines the to element as "# pcdata" Type
! Element from (the fifth line) defines the from element as the "# pcdata" type.
! Element heading (Row 6) defines the heading element as "# pcdata" Type
! Element body (Row 7) defines the body element as "# pcdata" Type
External Document declaration
If the DTD is located outside the XML source file, it should be encapsulated in a doctype definition using the following syntax:
<! Doctype root element system "file name">
This XML document is the same as the preceding XML document, but has an external DTD:
**************************************** *************************************
<? XML version = "1.0"?>
<! Doctype note system "note. DTD">
<Note>
<To> Tove </to>
<From> Jani </from>
<Heading> reminder <Body> don't forget me this weekend! </Body>
</Note> This is the "note. DTD" file containing the DTD:
<! Element note (to, from, heading, body)>
<! Element to (# pcdata)>
<! Element from (# pcdata)>
<! Element heading (# pcdata)>
<! Element body (# pcdata)>
**************************************** *************************************
Why use DTD?
With DTD, Each XML file can contain a description of its own format.
With DTD, independent groups can use a standard DTD to exchange data.
Your application can also use a standard DTD to verify the data received from the outside.
You can also use DTD to verify your data.
] 2. DTD-XML build Module
The main building modules of XML and HTML documents are labels like <body>... </body>.
XML document construction module
All XML documents (and HTML documents) are composed of the following simple building modules:
Element
Attribute
Entity
Pcdata
CDATA
The following is a brief description of each build module.
Element
Elements are the main building modules of XML and HTML documents.
Examples of HTML elements are "body" and "table ". Examples of XML elements are "NOTE" and "message ". An element can contain text, other elements, or null. Examples of empty HTML elements are "HR", "Br", and "IMG ".
Instance:
**************************************** *****************
<Body> body text in between </body>
<Message> some message in between </message>
**************************************** *****************
Attribute
Attribute provides additional information about the elements.
Attributes are always placed in the start tag of an element. The attribute is always in the form of name/value pairs. The following "IMG" element has additional information about the source file:
**************************************** *****************
**************************************** *****************
The element name is "IMG ". The property name is "src ". The attribute value is "computer.gif" and you can use a style to control the display effect such as the size. Because the element itself is empty, it is disabled by.
Entity
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.
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.
Pdata is the text that will be parsed by the parser. The text will be checked by the parser for entities and markup.
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 &, & lt;, and & gt; entities to replace them.
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.
3. DTD-Element
In a DTD, elements are declared through element declaration.
Declare an element
In DTD, XML elements are declared through element declaration. The element Declaration uses the following syntax:
<! Element name Category>
Or
<! Element element name (element content)>
Empty Element
Empty elements are declared using the class keyword empty:
<! Element element name empty>
Only pcdata Elements
Only pcdata elements are declared through # pcdata in parentheses:
<! Element element name (# pcdata)>
Elements with any content
Elements declared by category keyword any can contain a combination of any resolvable data:
<! Element element name any>
Elements with child elements (sequences)
The element with one or more child elements is declared by the child element name in parentheses:
<! Element element name (child element name 1)>
Or
<! Element element name (child element name 1, child element name 2,...)>
The same element must be declared at least once.
Syntax format:
<! Element element-Name (child-name +)>
Example:
<! Element note (Message +)>
In this example, the message of a sub-element must appear once or multiple times in the included note element.
Statement where the same element does not appear or appears multiple times
Syntax format:
<! Element element-Name (child-name *)>
Example:
<! Element note (Message *)>
In this example, the * sub-element message can appear in the contained note element for multiple times.
4. DTD-attributes
In DTD, attributes are declared through the ATTLIST declaration.
Declare attributes
Attribute Declaration supports the following syntax:
<! ATTLIST element name attribute type default value>
The following are the options of the attribute type:
Type description
The CDATA value is character data)
(EN1 | EN2 |...) this value is a value in the enumeration list.
The id value is a unique ID.
The idref value is the ID of another element.
List of idrefs values with other IDS
The nmtoken value is a valid XML name.
List of valid XML names with nmtokens values
The entity value is an entity.
The entities value is an Entity List.
Notation this value is the name of the symbol
XML: The value is a predefined XML value.
The default value can be the following:
Value explanation
Default Value of the Value Attribute
# The required attribute value is required.
# Implied attributes are not required
# The fixed value attribute value is fixed.
Specify a default property value
DTD:
<! Element square empty>
<! ATTLIST square width CDATA "0">
Legal XML:
<Square width = "100" type = "regxph" text = "yourobjectname"/>
In the preceding example, "square" is defined as an empty element with the "width" attribute of the CDATA type. If the width is not set, the default value is 0.
Advantages and disadvantages of DTD
Benefits of DTD
Each XML document can carry a DTD to describe the document format and test whether the document is a valid XML document. Since the DTD can be divided into external and internal, of course, you can define a public external DTD for an independent group, then multiple XML documents can share and use this DTD, this makes data exchange more effective. In some documents, internal DTD and external DTD can be combined. In the application, you can also use a DTD to check whether the received data meets a certain standard.
For XML documents, although DTD is not necessary, it brings convenience to document compilation. The consistency of parameters in the document tag is enhanced so that the XML syntax analyzer can confirm the document. If you do not use DTD to define an XML document, the XML syntax analyzer cannot confirm the document.
Element Definition
Root element Declaration
Each XML document has only one root element, and other child elements are contained in the root element. Therefore, the declaration of the root element in the DTD is indispensable. The general form of the root element declaration is as follows:
<! Doctype root [
<! -- Sub-element -->]>
<! -- File body -->
Doctype is short for "Document Type". The doctype Declaration must be placed at the top of the document. The doctype Declaration is an essential part of all codes and identifiers. The DTD syntax requires that doctype be capitalized, And the doctype and elements must be separated by spaces. For example, in the above Code, there must be spaces between doctype and root element root.
DTD Defects
Using a parser that verifies the validity of the DTD, you can immediately perform a reliable check on the integrity of the document. Although DTD is more practical, it also has many defects.