[XML] Learning Note (ii) Declaration and entity of--DTD

Source: Internet
Author: User
Tags mixed

I. Document TYPE declaration:
A) Declaration of the internal DTD:
The DTD definition statement is in the same document as the XML document, usually placed on the head. The format is as follows:

<! DOCTYPE root_tag[
<! ELEMENT xxx (...) > ...
] >


Note that the name of the root tag is followed by the DOCTYPE.
b) Declaration of the external DTD:
I.
<! DOCTYPE root_tag SYSTEM "*.dtd" >
<! ELEMENT xxx (...) > ...


Ii. parameter system indicates that the DTD document is a private DTD;
The parameter public indicates that the DTD document is a common DTD, and that a parameter "LIST", called the DTD name, is used to flag a DTD. Some applications work with DTDs by name first to their own DTD document library, and then to the path specified by the latter parameter when they cannot find it.
c) An external DTD is used in conjunction with an internal DTD:
<! DOCTYPE root_tag SYSTEM "*.dtd" [
<! ELEMENT xxx (...) > ...
] >


When internal DTDs and external DTDs conflict with the definition of a tag and the structure of the document, the definition of the internal DTD prevails.
The syntax of an element declaration: no declaration is forbidden.
A) #PCDATDA型数据: the content of the tag is resolvable text (text that does not contain markup), the mark defined as #pcdata is a leaf mark, and cannot have any child tags or other non-resolvable data content.
b) Any: when it is difficult to determine the type of a tag, it can be defined as any, but as sparingly as possible (contrary to the data structure of the XML document).
c) +: one or more;
*:0 one or more;
?: 0 or one;
(TAG1|TAG2) +:tag1 and tag2 can be any combination, as long as one appears at least once.
(Tag1, Tag2) *:tag1 and Tag2 as a whole, either at the same time, or not at the same time, and the number of occurrences and order to strictly conform to the definition. Equivalent to:
parenttag*
<! ELEMENT Parenttag (TAG1, Tag2) >
d) Mixed-type tagged content: You can have your own string content, or you can have a declaration of a child tag.
e.g. <! ELEMENT person (#PCDATA |name|addr|tel|email) *>
The token is a document content that can be parsed or a child tag in the list above, and the order and number of occurrences are arbitrary.
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE person [
<! ELEMENT person (#PCDATA |name|addr|tel|email) *>
<! ELEMENT name (#PCDATA) >
<! ELEMENT addr (#PCDATA) >
<! ELEMENT Tel (#PCDATA) >
<! ELEMENT Email (#PCDATA) >
]>
<person>chan
	<name>Jason</name>
	<addr> shanghai</addr>
	<tel>18701772821</tel>
	<email>1368628966@qq.com</email >
	<email>18701772821@163.com</email>
</person>


Note: You cannot use #pcdata as a special token.
such as <! ELEMENT person (#PCDATA, name, addr, tel, email) >
In fact, mixed-type tags undermine the highly structured document, which is not conducive to the processing of XML documents, should be avoided. <! ELEMENT person (#PCDATA |name|addr|tel|email) *> can be turned into
<! ELEMENT person (greet|name|addr|tel|email) *>
<! ELEMENT greet (#PCDATA) >
E) Empty Tags:
<! ELEMENT Blk empty>
<blk/> (or <blk></blk>)
f) Note:
<!--...-->
Iii. entities: Give a name to a piece of code or data so that it can be referenced elsewhere.
The generic entity refers to the text or data to be used in the future XML data document, and the parameter entity is a DTD information defined within the DTD document that represents part of the DTD definition, cannot be used in an XML document, and cannot be defined in a DTD.
A) Internal general entities: entities defined and used within the document.
A reference to an entity method starts with &, ends with, and the middle is the entity name.
e.g.
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE person [
<! ELEMENT person (name,addr,tel,br,email) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT addr (#PCDATA) >
<! ELEMENT Tel (#PCDATA) >
<! ELEMENT BR empty>
<! ELEMENT Email (#PCDATA) >
<! ENTITY Email "1368628966@qq.com" >
]>
<person>
	<name>Jason</name>
	< addr>shanghai</addr>
	<tel>18701772821</tel>
	<br></br>
	< Email>&email;</email>
</person>


Entities can also be called by other entities inside the DTD:
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE person [
<! ELEMENT person (name,addr,tel,br,email) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT addr (#PCDATA) >
<! ELEMENT Tel (#PCDATA) >
<! ELEMENT BR empty>
<! ELEMENT Email (#PCDATA) >
<! ENTITY correspondance "email:&email;" >
<! ENTITY Email "1368628966@qq.com" >
]>
<person>
	<name>Jason</name>
	< addr>shanghai</addr>
	<tel>18701772821</tel>
	<br></br>
	< Email>&correspondance;</email>
</person>


Note: A generic entity definition can only be applied to the definition of another entity, not in the definition of an element. In addition, you should also pay attention to the problem of dead loops referenced between entities.
b) External general entity: an entity defined outside the document entity that is to be referenced through a URL.
Represents a reference to an external general entity located in Test323.txt <! ENTITY content SYSTEM "Test323.txt" >
e.g.
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE person [
<! ELEMENT person (name,addr,tel,br,email) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT addr (#PCDATA) >
<! ELEMENT Tel (#PCDATA) >
<! ELEMENT BR empty>
<! ELEMENT Email (#PCDATA) >
<! ENTITY correspondance "email:&email;" >
<! ENTITY Email "1368628966@qq.com" >
<! ENTITY content SYSTEM "Test323.txt" >
]>
<person>
	<name>Jason</name>
	<addr>Shanghai</addr>
	&content;
</person>




Test323.txt:
<tel>18701772821</tel>
<br/>
<email>&correspondance;</email>


c) Internal parameter entity: Parameter entity cannot be applied in the declaration of element, cannot use parameter entity to define element, only parameter entity in external DTD can be applied to element declaration.
e.g.
Test323.dtd
<?xml version= "1.0" encoding= "UTF-8"?> <!
ELEMENT person (name,addr,tel,br,email) >
<! ENTITY%name "(#PCDATA)" >
<! ELEMENT addr%name;>
<! ELEMENT Tel%name;>
<! ELEMENT BR empty>
<! ELEMENT Email%name;>


Test323.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE person SYSTEM "TEST323.DTD" >
<person>
	<name>Jason</name>
	<addr> shanghai</addr>
	<tel>18701772821</tel>
	<br/>
	<email>18701772821@ 163.com</email>
</person>


Note: Parameter entities must be defined before they are used, not as arbitrarily placed as normal entities.
d) external parameter entities: it is possible to transform a very long DTD document into a small, mutually-callable collection of documents for the design and development of large DTD documents.
e.g.
Test323.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE person [
<! ELEMENT person (name,addr,tel,br,email) >
<! ENTITY% (note there is a space) content SYSTEM "Test323.dtd" >
%content;
] >
<person>
	<name>Jason</name>
	<addr>Shanghai</addr>
	<tel >18701772821</tel>
	<br/>
	<email>18701772821@163.com</email>
</ Person>


Test323.dtd
<?xml version= "1.0" encoding= "UTF-8"?> <!
ELEMENT name (#PCDATA) >
<! ELEMENT addr (#PCDATA) >
<! ELEMENT Tel (#PCDATA) >
<! ELEMENT BR empty>
<! ELEMENT Email (#PCDATA) >


Question: Can I define an internal parameter entity in the DTD document corresponding to an external parameter entity?

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.