DTD learning Summary

Source: Internet
Author: User
Tags cdata xml attribute
Document directory
  • Example:
  •  
  • Example:
1. Introduction to DTD

 

Document Type definition;

DTD is a type of XML constraint. In addition to this constraint, it also has xschema, xschema is more powerful than DTD constraints, so DTD is gradually eliminated;

The purpose of DTD is to constrain the writing of XML labels. For example, when writing struts. XML in struts2

<Struts>

<Package name = "" namespace = "" extends = ">

<Action>

</Action>

</Package>

</Struts>

The rules are constrained by DTD. Therefore, we use eclipse to import the DTD file during development to facilitate development. If developers do not follow the DTD specifications during development, an error message is displayed;

 

The importance of XML constraints: Constraints on XML are very important when writing XML, because it is difficult for developers to grasp XML constraints;

The DTD is usually defined by the Framework designer. Generally, developers only need to follow the DTD specification in XML to write XML;

Ii. Import DTD

 

To import a DTD, you can use either of the following methods:

(1) directly write in XML;

(2) write in a specialized DTD file, and then import the DTD file in XML using a specific statement;

 

1. directly write in XML

 

Syntax:

<! Doctype root node [...]>

2. Import the DTD file to the XML file

 

This method requires two considerations:

(1) if it is a local file, then:

<! Doctype root node System "dtd url">

The URL here refers to "F: \ 1.dtd"

(2) if it is a shared file, then:

<! Doctype root node public "DTD name" "dtd url">

Iii. DTD syntax

 

The sequence, number, attribute value, and entity of XML tags can be defined in DTD;

 

1. XML Element Definition

 

Syntax:

<! Element element name element type> // defines the element type, but can only be any (indicating any type) or empty (empty element );

<! Element element name (element content)> // define the content of the element. You must add ();

 

ForElement Content,

---- You can define the name of a child element, for example:

<! Element element name (child element 1, child element 2, child element 3)> indicates that three child elements can only appear once and appear in this order;

<! Element element name (child element 1 | child element 2 | child element 3)> indicates that only one of the three child elements can appear;

We can also define the number of child elements, for example:

(1) +: 1 time or multiple times; for example, (name +) indicates that this name element can only appear once or multiple times.

(2 )? : 0 or 1 time; for example, (name ?) This indicates that this name element can only appear 0 times or 1 time.

(3) *: 0 or multiple times. For example, (name *) indicates the number of times the name element may appear.

(4) None: Only one time; for example, (name) indicates that this name element can only appear once.

For element content,

---- You can also define the type of the child element content, for example, # pcdata, which indicates a string;

 

 

Example:

<! Doctype personlist [<! -- The root node is personlist --> <! Element personlist (person +)> <! -- The sub-element of the personlist is person and can appear once or multiple times --> <! Element person (name, age, salary)> <! -- The sub-elements of person are name, age, and salary. The sub-elements can appear once and appear in this order. --> <! Element name any> <! -- Name can be of any type --> <! Element age (# pcdata)> <! -- Age can be a string --> <! Element salary (# pcdata)> <! -- Salary can be a string -->]>

As can be seen from the above, DTD cannot define numbers and other types, which is a DTD defect;

 

2. XML Attribute Definition

 

Syntax:

<! ATTLIST Tag Name

Attribute name 1 attribute type attribute description

Attribute name 2 Description of attribute type

>

The property type can be

(1) CDATA: The most common string;

(2) enumeration, that is, (value 1 | value 2 | value 3), which requires parentheses

(3) ID: the attribute value must start with a letter or underscore;

The attribute description can be:

(1) # required: required;

(2) # impled: optional value, that is, this attribute can be set or not set;

(3) # fixed: fixed value. The general syntax is: # fixed "fixed value"

(4) default value;

Example:

<! Doctype personlist [<! Element personlist (person +)> <! Element person (name, age, salary)> <! Element name any> <! Element age (# pcdata)> <! Element salary (# pcdata)> <! ATTLIST personaaa CDATA # required <! --> Bbb cdata # implied <! -- Optional properties --> ccc cdata # fixed "xiazdong" <! -- Fixed value, no need to set --> ddd cdata "xzdong" <! -- The default value is "xzdong". You can set it by yourself --> eee id # required <! -- Id --> fff (1 | 2 | 3) "1" <! -- Enumeration value. The default value is 1. -->]>

 

3. XML entity definition

 

An object is similar to a reusable constant or classpath in ant and can be referenced;

Syntax:

<! Entity entity name "entity content">

Reference object Syntax:
& Entity name; note: the last ";" must be required;

 

Note: The referenced object must be referenced in XML;

 

Example:

<! Doctype personlist [<! Entity constant "aaaa"> <! Element personlist (person +)> <! Element person (name, age, salary)> <! Element name any> <! Element age (# pcdata)> <! Element salary (# pcdata)>]> <personlist> <person> <Name> & constant; </Name> <! -- Reference a constant object --> <age> AAA </age> <salary> AAA </salary> </person> </personlist>

TIPS: automatically generate a DTD Based on XML

Generally, the XML we write has no DTD. If you want to write your own DTD, It is very troublesome. Therefore, we can use a tool to automatically generate: trang

Function: converts data as follows:

(1) XML ---> DTD

(2) XML ---> xschema

(3) DTD ---> xschema

Usage:

Java-jar trang. jar-I XML-O DTD input. XML output. DTD;

-I XML indicates that the input is XML;

-O dtd indicates that the output is a DTD file;

Input. xml indicates the input file;

Output. DTD indicates the output file;

 

 

 

 

 

 

 

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.