XML (ii) effective XML:DTD (document type definition) Introduction and three ways to use

Source: Internet
Author: User

Effective XML:DTD (document type definition) Introduction

valid (Valid) XML document :

First, the XML document is a formal (well-formed) XML document;

Second, the requirements of the DTD need to be met, such an XML document is called a valid (Valid) XML document .

DTD

A DTD, which is a document type definition, is a texttype.

A DTD is used to describe the structure of an XML document.

  A DTD document contains:

The rules for defining elements (element);

The relationship rules between elements;

The definition of the attribute (attlist) rule;

The entity or symbol (NOTATION) rule that can be used.

the relationship between a DTD document and an instance of an XML document

  Classes and objects;

database table structure and data record;

With DTDs, each XML file can carry a description of its own format.

With DTDs, people from different organizations can use a common DTD to exchange data.

An application can use a standard DTD to verify that the XML data received from the outside world is valid.

You can use a DTD to validate your own XML data.

how to generate a DTD document

There are three application forms of DTD documents:

 1. Internal DTD Documentation

<! DOCTYPE root element [definition content] >

 2. External DTD documentation

<! DOCTYPE root element SYSTEM "DTD file path" >

 3. Integration of internal and external DTD documents

<! DOCTYPE root element SYSTEM "DTD file path" [Define content] >

Internal DTD

  An example of an internal DTD:

The upper part is a DTD, the following is an XML document, and the document conforms to the DTD.

  

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Poem [<! ELEMENT Poem (author, title, content)>    <!ELEMENT author (#PCDATA)>    <!ELEMENT title (#PCDATA)>    <!ELEMENT content (#PCDATA)>]><!--defines three child elements for element poem author title content, which must be present and must be in this order with fewer elements, no more elements -<!--indicates that the content inside the author,title,content is a string type -<Poem>    <author>Wang wei</author>    <title>Deer Firewood</title>    <content>The empty mountain disappears, but wenren the voice. Return to the scene into the deep forest, re-shining on the moss.</content></Poem>

the document above is well-formed and valid. See the comments.

#PCDATA(parsed Character data), the parsed character data, that is, the string.

The above section is a DTD, when writing an XML document below, if it does not conform to its DTD specification , when using XMLSpy for inspection, is well-formed, but it is not valid .

< Students > </ Students >

Validate the error when checking, the root element and the poem in the DTD do not match.

External DTD

Above is an example of an internal DTD, with an external DTD:

First, create a new DTD document named DTD_1.DTD, which reads as follows:

<? XML version= "1.0" encoding= "UTF-8" ?> <! ELEMENT Poem (author, title, content) > <! ELEMENT author (#PCDATA) > <! ELEMENT title (#PCDATA) > <! ELEMENT content (#PCDATA) >

Then, create a new XML document.

If you write only the XML element content, it is a well-formed, but not a valid document, because the DTD cannot be found.

The name of the external DTD file needs to be added (only the file name is given under the same path).

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE poem SYSTEM "Dtd_1.dtd"><Poem>    <author>Wang wei</author>    <title>Deer Firewood</title>    <content>The empty mountain disappears, but wenren the voice. Return to the scene into the deep forest, re-shining on the moss.</content></Poem>

internal and external DTD binding

The contents of the DTD can also be placed on the outside, some inside, as follows:

File Dtd_2.dtd

<? XML version= "1.0" encoding= "UTF-8" ?> <! ELEMENT author (#PCDATA) > <! ELEMENT title (#PCDATA) >

XML document:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE poem SYSTEM "Dtd_2.dtd" [<! ELEMENT Poem (author, title, content)><!ELEMENT content (#PCDATA)>]><Poem>    <author>Wang wei</author>    <title>Deer Wood</title>    <content>The empty mountain disappears, but wenren the voice. Return to the scene into the deep forest, re-shining on the moss.</content></Poem>

For example, the root element is written:

  

XML (ii) effective XML:DTD (document type definition) Introduction and three ways to use

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.