Document type definitions (DTDs) and entities (entity) for XML Learning notes

Source: Internet
Author: User
Tags cdata

Document type definition DTD
is part of the specification.
1 Why DTD is required

XML files not only follow the syntax rules of XML, but are also constrained by DTDs
The superiority of a DTD: a formal and precise definition of a glossary.
The role of DTDs: to provide the knowledge you know to the program, and to obtain documentation.

Only parsers with DTD and validation validity are required, and any errors in the document syntax, glossary, and specified values will not escape the parser's glasses.

2 Writing DTDs: General principles

An XML document consists of elements and corresponding attributes. The content of an element is defined by other elements or by the basic types specified in the XML standard.
DTD comparisons can define all the elements in a document, the attributes that an element can set, and the relationships between elements.
How DTDs are associated with XML
1 DOCTYPE Mark
The DOCTYPE declaration must be placed after the XML declaration and before any document element. However, the XML declaration and the DOCTYPE declaration can be inserted between interpretation and processing instructions.
The DOCTYPE declaration includes the keyword DOCTYPE. The document root element name, and the content declaration structure.
e.g
<? XML version= "1.0"?>
<! DOCTYPE Catelog ... >


The first line indicates that the document conforms to XML1.0, the second line indicates that the document uses the Catelog vocabulary, and catelog is the root element.

The keyword System parser will look for a DTD based on the URL given
Keyword public searching for DTDs based on URIs

Four markup declarations for DTDs
 ELEMENT    XML element type declarations
 ATTLIST    properties that can be set for a specific element type & The allowable value of the property declares
 ENTITY     reusable content declaration
 NOTATION   does not resolve the format declaration of the external content.

Solid entity
 
 
  Predefined entities     
    <                    &it;
    >                    &gt;
    &                    &amp;
    ' (single quotes)       &apos;
    (double quotes)      &quot;
 
  General entities
  Define a reusable copyright <! ENTITY Copyright "Megatrouble toys,inc.,1999";
  call requires a previous plus & followed by a semicolon, such as &copyright;
  When an entity reference is replaced with displacement text during parsing, the result must be a formal XML
  external form of a common entity
  e.g  <! entity myentity system  "Http://www.wror.com.boilper/copyright.txt" >



Unresolved external entities cannot be referenced in property values
An entity cannot contain a direct or indirect reference to itself

Parameter entity
The parsed entity that is used only in the DTD becomes the parameter entity. This makes it easy to reference or modify the structures commonly used in DTDs. However, when you extend the structure, you still need to edit the DTD.
Parameter entity declaration: Entity keyword, percent semicolon, name, and alternate word.
e.g <! ENTITY%peopleparameters "Age CDATE #IMPLIED Weight CDATA #IMPLIED Height CDATA #REQUIRED" >

CDATA represents character data.

In a DTD, all parameter entities must be declared before they are referenced. Therefore, the internal subset of the DTD cannot refer to the parameter entities declared in the outer subset, because the parser first reads the internal subsets.

When referencing a parameter entity, you need to add a percent semicolon before the entity name, followed by a semicolon.
e.g <! Attlist Insuredperson
%peopleparameters;
Carrier CDATA #REQUIRED >

The above code is equivalent to <! Attlist Insuredperson
Age CDATA #IMPLIED
Weight CDATA #IMPLIED
Height CDATA #REQUIRED
Carrier CDATA #REQUIRED >

This type of substitution-substitution in declarations-can only be used for external subsets of DTDs. In an internal subset, a parameter entity reference can only be placed between other declarations, so the replacement text referenced by such an entity must be a complete declaration, or it will affect the normality of the DTD format.

The parameter entity follows the document rule, and after replacing the entity reference with the replacement text, the document rule is compound.
The displacement text of a parameter entity can also be in an external text
e.g <! ENTITY%myparam SYSTEM "Http://www.worx.com/Wroxdecls.ent" >

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.