XML learning notes 2--DTD

Source: Internet
Author: User

In the previous document, when the document type was classified, the formatted XML and valid XML were differentiated based on whether the document was used and followed by the DTD or Schema, so What Are DTD and Schema? Both DTD and Schema are used to standardize XML documents and to impose semantic constraints on XML documents. DTD is easy to use and Schema is powerful. In this Document, first learn about DTD (Document Type Definition, Document Type Definition ).

1. How to Use DTD in XML documents

Import Method Syntax format Description
Internal DTD

Place the DTD definition in the XML document, followed by the XML declaration and processing instructions.

Example: <! DOCTYPE model list [

<! ELEMENT Model List (model) *>

]>

External DTD

Define the DTD separately in a file, and then import the DTD using the keyword SYSTEM

Example: <! DOCTYPE Model List SYSTEM "relative or absolute path of the DTD file in the Model List">

Common DTD

Public dtd. This DTD is generally designated by an authority for specific industries or the PUBLIC to use. It is imported using the PUBLIC keyword.

Example: <! DOCTYPE web-app PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">

2. Structure of DTD

The DTD document itself is not an XML document, but a document that defines semantic constraints for XML. the syntax of the DTD document is very simple and has the following structure:

(1) The first line is the declaration of the DTD document. The syntax is the same as the declaration of XML.

(2) 0 to multiple comments. The DTD comments have the same syntax as the XML comments.

(3) 0 to multiple <! ELEMENT...> definition, each <! ELEMENT...> define an XML ELEMENT

(4) 0 to multiple <! ATTLIST...> definition, each <! ATTLIST...> defines an attribute for the XML Element

(5) 0 to multiple <! ENTITY...> definition, each <! ENTITY...> define an object

(6) 0 to multiple <! NOTATION...> definition, each <! NOTATION...> define a symbol

<! ELEMENT...>, <! ATTLIST...>, <! ENTITY...>, <! NOTATION...> the Four definitions are completely independent of each other and do not need to be nested. The following describes these four definItions one by one.

3. Define Elements

(1) Element Type Definition (ETD)

(2) element type

Element type Definition Format Description
Any Type <! ELEMENT name ANY> The element can be a string, a null element, or a child element.
String Value <! ELEMENT name (# PCDATA)> The element value can only be a string, not a null element or a child element.
Empty Element <! ELEMENT name EMPTY>  
Contains child elements   Complex. You need to define the sequence between child elements and the number of times the child elements appear.
Hybrid Type <! ELEMENT name (# PCDATA | child ELEMENT 1 | child ELEMENT 2 |...) *> The specified value can only be a few definite types, which is more restrictive than any type, but the function is equivalent. Try to use a hybrid type.

The following table describes the definitions of hybrid types:

A: # PCDATA must be placed at the beginning

B: # use a vertical bar (|) to separate PCDATA and child elements. Do not use a comma to separate them.

C: Do not use it after the child element? , *, +, And so on.

(3) define child elements

Syntax for defining child elements Description Occurrence Frequency modifier of child elements Description
(Child element 1, child element 2 ,...) Use commas to define ordered child elements Default (no modifier) Appears once, and can only appear once
(Child element 1 | child element 2 | ...) Use a vertical bar to define mutually exclusive child elements ? 0 or 1 occurrence
(Child element 1, child element 2) | (child element 3, child element 4 )) Use parentheses to group child elements + Appears 1 or multiple times
(Child element 1 | child element 2 |...) + Use vertical bars to mutex, and then use frequency modifiers to implement unordered sub-elements. * 0 or multiple times

4. Define attributes

In XML, attributes cannot exist independently. Therefore, you must specify the element to which the attribute belongs. The syntax format for defining attributes is as follows:

 

(1) attribute type

Type Description
CDATA This attribute value can only be string data.
(En1 | en2 | en3) This attribute value must be one of a series of enumerated values.
ID The property value must be an identifier and can be used to identify the element. Therefore, it must be unique in this XML document.
IDREF This attribute value must be an attribute value that references another existing ID type.
IDREFS This attribute value must be an attribute value that references one or more existing ID types. The attribute values of Multiple ID types are separated by spaces.
NMTOKEN This attribute value must be a valid XML name and must be string data. It is more restrictive than CDATA and can only contain letters, numbers, underscores, dashes, periods, and colons.
NMTOKENS This attribute value must be one or more NMTOKEN type attribute values. Multiple values are separated by spaces.
ENTITY This attribute value is an external entity, such
ENTITIES This attribute value is one or more ENTITY type attribute values, separated by Spaces
NOTATION This property value is a symbol (NOTATION) that has been declared in the DTD. This is a specification to be expired and should be avoided as much as possible.
Xml: The property value is a predefined XML value.

(2) Relationship between element constraints on attributes and default values

Element constraints on Attributes Description Default Value
Not Specified   The default value must be specified.
# REQUIRED Required attribute. This attribute must be provided for the corresponding element. The default value cannot be specified.
# IMPLIED This attribute is optional The default value cannot be specified.
# FIXED The property value is fixed and must be specified during definition. The default value must be specified.

5. Define entities

Entity reference is to replace another string with one character string. It is similar to a macro in C language. The five entity references built in XML have been mentioned in the previous note. Here, let's take a look at how to customize entity references.

Entity type Places of use Definition syntax Use syntax Description
Normal entity XML & Entity name;  
Parameter entity DTD % Entity name; Must be defined before use
External entity XML & Entity name; Here, the external file must be a text document that meets the XML document structure.
Public external entity XML & Entity name;  
External parameter entity DTD % Entity name;  
Public external parameter entity DTD % Entity name;  
Unresolved entity XML It needs to be called through ENTITY and other types of attributes Unresolved entities cannot be parsed by XML documents, but need to be parsed according to the corresponding symbol name
Public unresolved entities XML

6. Define symbols

There are two syntax formats for defining symbols:

Symbol type Definition syntax
Common symbols
Common symbols

Symbol values usually take two forms:

(1) MIME: files of the common MIME type are always processed by corresponding programs.

(2) external program path: specify an external program to process external data in the XML document.

A symbol has two purposes:

(1) As above, symbols can be used to define unresolved entities

(2) A symbol can be used as an attribute value of the ENTITY or ENTITIES type.

(3) the symbol can also be used as the value of the NOTATION type attribute. when defining the NOTATION type attribute, the syntax is as follows:

 

A list with one more value than the general attribute definition.

 

Related Article

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.