To define a DTD for an XML file

Source: Internet
Author: User
Tags cdata mixed

A DTD is an abbreviation for the document type definition (the definition of a file). It guarantees the legitimacy of XML documents by defining rules such as elements, attributes, tags, and entities in the document and their interrelationships.

1.DTD declaration mode is divided into internal and external declarations

The format of the internal declaration is <! DOCTYPE root element [

<! element root elements (element 1, element 2) >

<! Element 1 (#PCDATA) >

<! Element 2 (#PCDATA) >

]>

The format of the external declaration is divided into two types, the corresponding keyword is "SYSTEM" and "public" <! DOCTYPE root element SYSTEM "external DTD file" >

<!--the external DTD file here, if the absolute path is not specified, it must be in the same directory as the XML file, otherwise invalid-->

A DTD file for "public" declarations is usually a DTD that is made by an authoritative organization and is available to a particular industry or public. <! DOCTYPE root element Public "external DTD name" "External DTD file path" >

2. Element type declaration <! element element name elements Content Description >

The element content types used in DTDs are: EMPTY, any, Mixed, Elements

Empty refers to elements that cannot have any content, but can have attributes: <!--DTD declarations section-->

<! Element elements 1 EMPTY >

<! attlist element 1 Sex (male | female) "Male" >

<!--XML File part-->

< element 1 gender = "female"/>

<!--here is the 1> of the < element 1></element, and even if there is no data in it, an error can occur-->

Any description element can have any type of child element, or it can be plain text, or it can be an empty <! Element 2 any >

It is important to note here that although an element defined with any can contain other elements, it must follow the "valid" principle of the XML file that any elements used in the file must be defined in the DTD

Look at the illegal document below:

? XML version= "1.0" encoding= "gb2312"?>

<! DOCTYPE Book information [

<! ELEMENT Book information any >

]>

< book information >

< title > New concept English </title >

</Book Information >

The compiler prompts such an error message

"This file isn't valid:element ' title ' has not been declared."

is to suggest that the element "book title" is not defined, and to correct this error, simply add the definition statement to the DTD definition section "<! ELEMENT title (#PCDATA) > "on it.

Mixed allows mixed content to allow character data and other elements to coexist within an element, not in the form of a keyword <! Element 3 (#PCDATA | child element 1| 2) >

? XML version= "1.0" encoding= "gb2312"?>

<! DOCTYPE Book information [
<! ELEMENT book information (#PCDATA | title | price) * >
<! ELEMENT title (#PCDATA) >
<! ELEMENT Price (#PCDATA) >
]>

< book information >
New to book Today:
< title >
XML Practical Tutorials
</title >
< price >
¥26.00
</Price >
</Book Information >

Elements specify that specific child elements must appear in rules and order, and that child elements can use various meta characters to indicate the number of occurrences

<! Element 4 (child element 1, child element 2, child element 3) >

Metacharacters that may occur:

Metacharacters Meaning
+ Occurs 1 or more times
* Occurs 0 or more times
? Occurs 0 or 1 times
No sign can only appear 1 times

In the following example, the "It job search" elements must appear in order and the number of meta character descriptions. XML version= "1.0" encoding= "gb2312"?>

<! DOCTYPE IT Job [
<! ELEMENT It Job (recruitment information) + >
<! ELEMENT Recruitment (company name, job title +, company website, contact info *) >
<! ELEMENT company Name (#PCDATA) >
<! ELEMENT jobs (#PCDATA) >
<! ELEMENT company website (#PCDATA) >
<! ELEMENT Contact Method (#PCDATA) >
]>

< IT job >

< recruitment information >
< company name > ZTE </Company name >
<!--there have been 2--> jobs here.
< job > 1.Java Senior Engineer </Recruitment position >
< job > 2.c++ Senior Engineer </Recruitment position >
<!--the company's website appears 1 times-->
< company website > http://www.zte.com.cn/</company website >
<!--here Contact--> 2 times.
< contact > Address: Shenzhen South Mt. District Hi-Tech Industrial park zhongxing Communication building </Contact Way >
< contact > Tel: 0755-26770000 </Contact Way >
</Recruitment Information >

< recruitment information >
< company name >
Alibaba
</Company Name >
<!--There have been 1--> jobs here.
< job > Software Test Engineer </Recruitment position >
<!--did not appear on the company website-->
<!--did not appear contact way-->
</Recruitment Information >

</IT Job >

3. Define valid element attributes

When defining attributes in a DTD, we use the following format:

<! Attlist element Name Property name (property Value property type default value) *>

For example:

<! Attlist author

Name CDATA #REQUIRED

Sex (male | female) "Male"

How to contact CDATA #IMPLIED

>

Depending on whether the XML file must provide a value for a property, the default value of the property can be grouped into the following four categories: properties that must be assigned required
Attribute values are optional properties implied
fixed-Value property fixed
Custom Default Values

Use an example below to illustrate the four class default properties, "xml version=" 1.0 " encoding=" gb2312?>

<! doctype  book information  [
<! element  Book information   (title, author, price) * >
<! element  title   (#PCDATA) >
<! element  author   (#PCDATA) >
<! element  Price   (#PCDATA) >
<! attlist  author  
Name  CDATA  #REQUIRED
Gender   (male | female)   "male"
Contact  cdata # Implied
Classification  CDATA  #FIXED   Web programming Technology
>
]>

< book information >
< title > ASP. NET case Development Collection </title >
< author   name = "Sean, Yang Limin"/>
< price > 37.00 </price >

< title > C # Advanced Programming </title >
< author   name = "Wrox"   Contact way = "http://www.wrox.com"/>
< price > 128.00 </Price & Gt

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.