XML series: Document Type Definition DTD)

Source: Internet
Author: User
Tags xml example

1. What is DTD?
1. XML is a metadatabase language that defines the syntax structure of tags to generate new tags. The DTD creates a document for the new mark and provides a standard description.
That is to say, the syntax structure of the XML definition mark is defined by DTD and the specification of the document structure is specified, while the XML document uses the mark.
2. Specification of Self-describing data.
 
Ii. Role of DTD
1. DTD defines the elements (tags and attributes) and entities in the document and their relationships. Data and structure separation.
2. verify the validity of the XML document through DTD. DTD provides a basis for the parser to parse XML documents. Therefore, each XML document must specify which DTD is valid.

Iii. Structure of DTD
 1. Element Declaration: Use element declaration to declare elements in XML documents

(1) elements with data:
<! Element element name (data type)>
Data Type:
# CDATA: the element contains character data that is not parsed by the parser. Special characters and reserved words do not need to be escaped.
# Pcdata: contains the parser that can parse character data. Special characters and reserved words need to be converted to the parser.
Any: an element can contain sub-elements and character data of any declared type.

 (2) elements with child elements:
<! Element element name (child element 1, child element 2)>
Multiple child elements are separated by commas.
The order in the document is the same as that in the definition.
Child elements can have their own child elements.

① The same element appears only once:
<! Element element name (child element)>
② The same element should appear at least once: +
<! Element element name (child element +)>
③ Zero or multiple occurrences of the same element :*
<! Element element name (child element *)>
④ Zero or one occurrence of the same element :?
<! Element element name (child element?)>

 (3) Empty element:
<! Element element name (empty)>
Null elements can have attributes.

(4) hybrid statement
A group can be a sequence or a selection of sub-elements and/or sub-groups:
1. Sequence
<! Element A (B)>: Element A consists of a single sub-element B.
<! Element A (B, c)>: Element A consists of child elements B and C.
<! Element A (B, (C | D), E>: Element A consists of child elements B, E, and select Sub-groups (one of C or D.
② Select sub-elements and/or sub-groups
<! Element A (B | C)>: Element A consists of child element selection sub-groups (B or C.
<! Element A (B | c | (D, E)>: Element A is selected by including Sequence Sub-Groups (D and E) (B or C or D, one of E) composition.

2. Attribute Declaration
(1) Empty attributes
<! Atttlist element name empty>
(2) non-empty attributes
<! ATTLIST element name attribute type attribute value>
Property value:
① Default property value: specify a default value
<! ATTLIST element name attribute type "Default Value">
DTD example:
<! The price of ATTLIST is CDATA "RMB">
XML example:
<Selling price currency unit = "RMB"> 10.90 </selling price>
② Implied property value: this property is not available and has no default value.
<! ATTLIST element name attribute type # implied>
DTD example:
<! ATTLIST price currency unit CDATA # implied>
XML example:
<Price> 10.90 </price>

③ Required property value: This property must be provided, but can have no default value.
<! ATTLIST element name attribute type # required>
DTD example:
<! The price of ATTLIST is CDATA # required>
XML example:
<Selling price currency unit = "USD"> 10.90 </selling price>

④ Fixed property value: make the property have a fixed value and cannot be changed.
<! ATTLIST element name attribute type # fixed "fixed value">
DTD example:
<! The price of ATTLIST is CDATA # fixed "RMB">
XML example:
<Selling price currency unit = "RMB"> 10.90 </selling price>

Property type:
①. Enumerated attribute type: Make the default value one of the fixed values.
<! ATTLIST element name attribute name (fixed value a | fixed value B |...) One of the default fixed values>
DTD example:
<! ATTLIST price: Currency Unit ("RMB" | "USD" | "Euro") "RMB">
XML example:
<Selling price currency unit = "Euro"> 10.90 </selling price>

②. ID and idref attribute types
1. ID: used to search for a specific instance of an element. Each element can have an id attribute..
<! ATTLIST element name attribute name ID attribute value>
DTD example:
<! ATTLIST Price Payment Method ID # required>
XML example:
<Price id = "book1"> 10.90 </price>
<Price id = "book2"> 10.90 </price>
<Price id = "book3"> 10.90 </price>
2. idref: points to an element that references one of the other elements.
<! ATTLIST element name attribute name idref attribute value>
DTD example:
<! ATTLIST price currency unit ID # required>
<! ATTLIST price currency unit idref # implied>
<! ATTLIST price currency unit CDATA # implied>
XML example:
<Price id = "book1" currency unit = "RMB"> 10.90 </price>
<Price id = "book2" idref = "book1"> </price>
③ Idrefs attribute type
Point to multiple element IDs, separated by spaces. It is used to point to the list of related elements in the XML document.
<! ATTLIST element name attribute name idrefs attribute value>

4. Use DTD
 
1. Internal DTD
After the XML declaration, write the internal dtd in the xdoctype declaration.
<! Doctype root element name [
Element Declaration
...
]>
2. External DTD
Write a DTD file with the extension of DTD. Add a dtd reference to the doctype Declaration of the XML document to inform the parser of the external DTD information,
<! Doctype root element name system "DTD file name. DTD">
 
3. Internal and external entities and

See XML series: entities in the XML document structure;

4. Parameter entities in DTD
Use the internal DTD declaration to set the parameter entity as the element name and reference the external DTD for verification in the XML file.

DTD file
...
<! Entity % entity name "character data">
<! Element element name (% entity name)>
<! Element % Object Name (child element)>
...

XML file
...
<! Doctype root element name system "DTD file name. DTD "[
<! Entity % entity name "replacing element name of entity name">
]>
...
 
Example: XML of different elements are associated with a DTD file.

DTD file
...
<! Entity % element "E">
<! Element root (% element)>
<! Element % element (name, sex,...)>
...
 
XML file 1
...
<! Doctype root system "DTD file name. DTD "[
<! Entity % element "student">
]>
<Root>
<Student>
<Name> Michael Zhang </Name>
...
<Student>
</Root>

XML file 2
...
<! Doctype root system "DTD file name. DTD "[
<! Entity % element "teatcher">
]>
<Root>
<Teatcher>
<Name> instructor Wang Wu </Name>
...
<Teatcher>
</Root>

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.