DTD (document type definition)

Preface: tell you about dtd

Codeword is not easy. Pay attention

Reprint, please explain!

Mind map:

catalogue

1. What is DTD?  

2. What is well formed xml

1. What is xml?

2. Function of XML

3. Standard xml format

3. Element definition

4. Attribute definition

5. Write a DTD restriction servlet related configuration (example)

6. Write a DTD limit config Example (XML)

1. What is DTD?  

Document type definition is a set of syntax rules for markers established for data exchange between programs. It is part of the standard General Markup Language (SGML) and extensible markup language (XML) version 1.0 specifications, and documents can verify whether the format conforms to this rule according to a DTD syntax rule. The document type definition can also be used to ensure the legitimacy of the standard General Markup Language and extensible markup language document format. You can check whether the document conforms to the specification and whether the elements and labels are used correctly by comparing the document and the document type definition file. File instances provide an application with a format for data exchange
PS: in short, DTD is used to constrain XML documents and make them used under certain specifications. In addition to DTD technology, Schema technology is also used to constrain XML documents

2. What is well formed xml

1. What is xml?

Extensible markup language

2. Function of XML

1. Data interaction

2. Configuration

3. Standard xml format

1. There is only one root element

2.XML tags are case sensitive

3. Use the end tag correctly

4. Use nested labels correctly

5. Use legal tag name

6. Define valid attributes

The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person pid="p1" sex="male" qq="aaa" parent="p2">
		<name>Zhang San</name>
		<age>10</age>
		<contact>
			<phone>1234567</phone>
		</contact>
		<br/>
	</person>
	<person pid="p2">
		<name>Li Si</name>
		<age>35</age>
		<contact>
			<email>123@qq.com</email>
		</contact>
	</person>
</persons>

3. Element definition

1. Introduce DTD constraint

<!DOCTYPE persons[]>

2. Add element definition

(* lowercase to uppercase Ctrl+Shift+x)

<! Element br empty > empty element

<! Element name (#pcdata) > text element

<! Element element name (E1, E2) > mixed elements

3. Restriction of elements

*And (,) not (|)

Times:

0 or 1:?

0~N:*

1~N:+

The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!-- introduce DTD constraint -->
<!DOCTYPE persons[
<!-- Add element definition   Ctrl+shift+x Lowercase to uppercase-->
 <!ELEMENT persons (person+)>
 <!ELEMENT person (name,age,contact,br?)>
 <!ELEMENT name (#PCDATA)>
 <!ELEMENT age (#PCDATA)>
 <!ELEMENT contact (phone|email)>
 <!ELEMENT br EMPTY>
]>
<persons>
	<person>
		<name>Zhang San</name>
		<age>10</age>
		<contact>
			<phone>1234567</phone>
		</contact>
		<br/>
	</person>
	<person>
		<name>Li Si</name>
		<age>35</age>
		<contact>
			<email>123@qq.com</email>
		</contact>
	</person>
</persons>

 4. Attribute definition

1. Grammar

<!ATTLIST person>

2. Attribute type

  • ID
  • (male and female)
  • CDATA text type
  • IDREF dependency ID
  • REFERENCE

3. Attribute description

  • #REQUIRED: REQUIRED
  • #IMPLIED: not required
  • 'default value 'note: desc can use the default value only when the type is (male | female)

The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!-- introduce DTD constraint -->
<!DOCTYPE persons[
<!-- Add element definition   
Ctrl+shift+x Lowercase to uppercase
IDREF Depend on ID category

-->
 <!ELEMENT persons (person+)>
 <!ELEMENT person (name,age,contact,br?)>
 <!ELEMENT name (#PCDATA)>
 <!ELEMENT age (#PCDATA)>
 <!ELEMENT contact (phone|email)>
 <!ELEMENT br EMPTY>
 <!ATTLIST person
      pid ID #REQUIRED
      sex (male|female) 'male'
      qq CDATA #IMPLIED
      parent IDREF #IMPLIED
 >
]>

<persons>
	<person pid="p1" sex="male" qq="aaa" parent="p2">
		<name>Zhang San</name>
		<age>10</age>
		<contact>
			<phone>1234567</phone>
		</contact>
		<br />
	</person>
	<person pid="p2">
		<name>Li Si</name>
		<age>35</age>
		<contact>
			<email>123@qq.com</email>
		</contact>
	</person>
</persons>

 

5. Write a DTD restriction servlet related configuration (example)

The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!-- introduce DTD constraint -->
  <!DOCTYPE web-app [
<!-- Add element definition   Ctrl+shift+x Lowercase to uppercase--> 
    <!-- Text constraints -->
    <!ELEMENT servlet-name (#PCDATA)>
    <!ELEMENT servlet-class (#PCDATA)>
    <!ELEMENT url-pattern (#PCDATA)>
    <!-- Element classification -->
    <!ELEMENT web-app (servlet*,servlet-mapping*)>
    <!-- Mixed elements -->
    <!ELEMENT servlet (servlet-name,servlet-class)>
    <!ELEMENT servlet-mapping (servlet-name,url-pattern+)>
]>
<web-app>
  <servlet>
  <servlet-name>PersonsServlet</servlet-name>
  <servlet-class>com.hpw.dtd.PersonsServlet1</servlet-class>
  </servlet>
  <servlet>
  <servlet-name>PersonsServlet1</servlet-name>
  <servlet-class>com.hpw.dtd.PersonsServlet2</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>PersonsServlet</servlet-name>
  <url-pattern>Persons1.jsp</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  <servlet-name>PersonsServlet1</servlet-name>
  <url-pattern>Persons2.jsp</url-pattern>
  <url-pattern>Persons3.jsp</url-pattern>
  </servlet-mapping>
</web-app>

6. Write a DTD limit config XML (example)

The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!-- config label:Can contain 0~N individual action label -->
	<!DOCTYPE config[
	<!ELEMENT config (action*)>
	<!ELEMENT action (forward*)>
	<!-- action label:Can be full of 0~N individual forward label path:with/String beginning with,
	And the value must be unique and non empty ,Path corresponding to sub controller type:character string,Non empty, full class name of the child controller -->
	<!ATTLIST action
      path CDATA #REQUIRED
      type CDATA #REQUIRED
      
 >
    <!ATTLIST forward 
      name CDATA #REQUIRED
      path CDATA #REQUIRED
      redirect (false|true) 'true'
    >
]>
<config>

	<action path="/registerAction" type="test.action.RegisterAction">
		<forward name="success" path="/index.jsp" redirect="true" />
		<forward name="failed" path="/register.jsp" redirect="false" />
	</action>
	<action path="/loginAction" type="test.action.LoginAction">
		<forward name="a" path="/index.jsp" redirect="false" />
		<forward name="b" path="/welcome.jsp" redirect="true" />
	</action>
</config>

IT's over here. I'm still the primary school student studying IT

You are welcome to give advice

Added by Schneider707 on Tue, 04 Jan 2022 20:50:57 +0200