Basic XML knowledge point 2-dtd

Source: Internet
Author: User
Tags xml parser

Document Type Definition (DTD) can define legal XML document building modules. It uses a series of legal elements to define the structure of documents. It can be seen as the data structure of XML documents, which can form a reliable error monitoring mechanism. Programmers or parsers can find possible errors. If you do not specify a DTD, the program can run (XML can be parsed), but you cannot determine that the data in XML fully meets the requirements. Sometimes incorrect data may lead to inexplicable errors.

For more information, see the DTD tutorial.

Example:

 1 <?xml version="1.0"?> 2 <!DOCTYPE note [ 3   <!ELEMENT note (to,from,heading,body)> 4   <!ELEMENT to      (#PCDATA)> 5   <!ELEMENT from    (#PCDATA)> 6   <!ELEMENT heading (#PCDATA)> 7   <!ELEMENT body    (#PCDATA)> 8 ]> 9 <note>10   <to>George</to>11   <from>John</from>12   
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 note elements: "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

Note: When sub-elements are declared in a sequence separated by commas, these sub-elements must appear in the document in the same order. In a complete declaration, sub-elements must also be declared, and sub-elements can also have sub-elements.

The Data Types of elements include:

# CDATA: (character data) indicates that an element contains character data that is not parsed by a parser. Special characters and reserved words do not need to be escaped.
# Pcdata: (parsed CDATA) indicates that the element includes the parser that can parse character data. Special characters and reserved words need to be converted to the parser.
Any: an element can contain sub-elements and character data of any declared type.

If it is an empty element, such as <Note> </Note> or <Note/>, it is defined as follows:

<!ELEMENT note EMPTY>

 

 

If the DTD is located outside the XML source file, it should be encapsulated in a doctype definition using the following syntax, as shown below:

1 <?xml version="1.0"?>2 <!DOCTYPE note SYSTEM "note.dtd">3 <note>4 <to>George</to>5 <from>John</from>6 

This is the "note. DTD" file containing the DTD:

1 <!ELEMENT note (to,from,heading,body)>2 <!ELEMENT to (#PCDATA)>3 <!ELEMENT from (#PCDATA)>4 <!ELEMENT heading (#PCDATA)>5 <!ELEMENT body (#PCDATA)>

 

The DTD declaration can also control the number of occurrences of elements and multiple options, as shown below:

<!ELEMENT team (people+,name*,gender?,(message|body))>

Team indicates that only one appears, People + indicates that at least one appears, name * Indicates 0 appears or multiple times, gender? Indicates 0 or 1 occurrence, and (Message | body) indicates either one of the message and body elements.

 

In addition to declaring elements, you can also declare attributes in the format of <! ATTLIST element name attribute type default value>

As follows:

 1 <?xml version="1.0"?> 2 <!DOCTYPE team [ 3     <!ELEMENT  team (people+)> 4     <!ELEMENT people (name,gender)> 5     <!ELEMENT name (#CDATA)> 6     <!ELEMENT gender (#CDATA)> 7     <!ATTLIST people type CDATA "employee"> 8 ]> 9 10 <team>11     <people type="employee">12        <name>zhangsan</name>13        <gender>f</gender>14     </people>15     <people type="leader">16        <name>lisi</name>17        <gender>m</gender>18     </people>19 </team>

The options for Attribute types are as follows:

Type Description
CDATA The value is character data.
(EN1 | EN2 | ...) This value is a value in the enumeration list.
ID The value is a unique ID.
Idref The value is the ID of another element.
Idrefs List of other IDS
Nmtoken The value is a valid XML name.
Nmtolens List of valid XML names
Entity Value is an entity
Entities Returns an object 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 Description
Value Default Attribute Value
# Required Attribute values are required
# Implied Property is not required
# Fixed value The property value is fixed.

 

 

 

 

 

An example of external entity reference is as follows:

Lele1.xml

<?xml version="1.0"?><people id="1">    <name>zhangsan</name>    <gender>f</gender></people>

Lele2.xml

1 <?xml version="1.0"?>2 <people id="2">3     <name>lisi</name>4     <gender>m</gender>5 </people>

Team. xml

1 <?xml version="1.0"?>2 <!DOCTYPE team [3     <!ENTITY people1 SYSTEM "./people1.xml">4     <!ENTITY people2 SYSTEM "./people2.xml">5 ]>6 <team>7     &people1;8     &people2;9 </team>

The final XML document is as follows:

 1 <?xml version="1.0"?> 2 <team> 3     <people id="1"> 4         <name>zhangsan</name> 5         <gender>m</gender> 6     </people> 7     <people id="2"> 8         <name>lisi</name> 9         <gender>f</gender>10     </people>11 </team>    

Note: Due to the injection vulnerability of external entities, the XML Parser references external resources as entity content in the current context environment, in the actual application context, this part of data is introduced into the logical process for processing. So currently, some parsers do not parse external entities. It is best to check the underlying XML parsing library used. By default, external entity Parsing is prohibited, and system monitoring is enhanced to prevent this problem from being exploited.

Basic XML knowledge point 2-dtd

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.