DTD constraints: Declarations and references to DTDs
1. Internal DTD Documentation
<! DOCTYPE root element [definition content]>
2. External DTD documentation
" DTD file path "> or "DTD name ""URL for DTD file">
3, thebasic syntax of DTD elements: <! element name category > or <! Element elements name (element content) >
-element is a keyword and cannot be modified
- element type, must be capitalized! Content is written in three ways:
(1) empty--indicates that the element cannot contain child elements and text, but can have attributes. (2) any--indicates that the element can contain any element content defined in the DTD (3) #PCDATA--can contain any character data, but cannot contain any child elements
combination type, the following modifiers are available:
symbols |
Use |
Example |
Example Description |
() |
Used to group elements |
(Dragon | Jin Yong), (Wang Shuo | yu jie) |
Divided into two groups |
| |
In the list of objects, select a |
(Men | women) |
means that a man or woman must appear, at least one of them. |
+ |
The object must appear one or more times |
(member +) |
Indicates that a member must appear, but multiple members can appear |
* |
This object allows to occur 0 or more times |
(Hobby *) |
Hobbies can appear two times to many times |
? |
The object must appear 0 or 1 times |
(rookie?) |
Rookie can appear, also can not appear, if appear, can appear at most only once |
, |
Objects must appear in the order specified |
(Watermelon, apple, banana) |
Watermelon, apples, bananas must appear, and appear in this order
|
For example:
<! ELEMENT Family (person +, home appliance *) >
<! ELEMENT to empty>
<! ELEMENT Body (#PCDATA) >
4, the definition of attributes:
Attribute name Type attribute characteristics
... >
Type of property: (1) CDATA (2) ID (3) idref/idrefs (4) enumerated (5) Entity/entities
(1) attribute type-cdata, which indicates that the property value can be any character (including Chinese and numeric)
(2) The attribute type-id, which indicates that the value of the property must be unique, but the value of the property cannot start with a number!
(3) attribute type-idref/idrefs
(4) attribute type-enumerated, define some values beforehand, the value of the property must be within the range of the listed values.
(5) attribute type-entity, entity
Entity definition:
-The entity is used to create an alias for a piece of content that can be referenced in an XML document later in the use of an alias.
-In the DTD definition, one! The entity statement is used to define a body.
-entities can be divided into two types: reference entities and parametric entities. The referencing entity is applied by the XML document, and the parameter entity is applied by the DTD file itself.
① Referencing entities:<! Entity entity name "Solid content" >
" I am a programmer "> .... ©right;
② parameter entity:<! Entities% entity name "entity Content" >
" name | email| Telephone | address "><! ELEMENT Personal information (%tag_name;| birthday) ><! ELEMENT Customer information (%tag_name;| company name) >
Characteristics of the property:
(1) #REQUIRED, indicating that this attribute must be given, without giving an error
(2) #IMPLIED, indicating that this attribute can be given or not given
(3) #FIXED value, which indicates that the attribute must give a fixed value
(4) Default value, which indicates that if there is no value for this property, a default value value is assigned
DTD actual case:
DTD file:
<! ENTITY AUTHOR"John Doe"><! ENTITY Company"JD Power Tools, Inc."><! ENTITY EMAIL"[email protected]"><! ELEMENT CATALOG (product+) ><!ELEMENT PRODUCT (Specifications+,options?,price+,notes?) ><!attlist PRODUCTNAME CDATA #IMPLIEDCATEGORY (Handtool| table| shop-professional)"Handtool"PARTNUM CDATA #IMPLIEDPLANT (Pittsburgh| milwaukee| Chicago)"Chicago"INVENTORY (Instock| backordered| Discontinued)"Instock"><! ELEMENT Specifications (#PCDATA) ><!attlist specificationsweight CDATA #IMPLIEDPOWER CDATA #IMPLIED><! ELEMENT OPTIONS (#PCDATA) ><!attlist optionsfinish (Metal| polished| Matte)"Matte"ADAPTER (Included| optional| notapplicable)"Included"Case (Hardshell| Soft| notapplicable)"Hardshell"><! ELEMENT Price (#PCDATA) ><!attlist PRICEMSRP CDATA #IMPLIEDWHOLESALE CDATA #IMPLIEDSTREET CDATA #IMPLIEDSHIPPING CDATA #IMPLIED><! ELEMENT NOTES (#PCDATA) >
the corresponding XML:
<?xml version="1.0"encoding="Utf-8"? ><! DOCTYPE CATALOG SYSTEM"PRODUCT.DTD"><CATALOG> <product name="kang Shuai fu mineral water"category="Table"partnum=" A"Plant="Chicago"> <specifications weight=" -"Power=" -"> Here is the details </SPECIFICATIONS> <PRICE> -</PRICE> <PRICE> -</PRICE> </PRODUCT></CATALOG>
DTD constraints FOR XML