dtd specification

Reprinted from: https://www.cnblogs.com/xiaofu007/p/5079428.html

 

filename student.xml

1 <?xml version="1.0" encoding="GB2312" standalone="no"?>
 2 <!--Hello everyone, I'm a comment-->
 3 <!--2015.12.27-->
 4 <!DOCTYPE students SYSTEM "student.dtd">
 5 <!--The drive letter is not written here, and it means the current directory, that is to say, the two files (student.xml and student.dtd) should be put together -->
 6
 7 <students>
 8     <student number="ITCAST_1001">
 9         <name>张三</name>
10         <age>23</age>
11         <sex>male</sex>
12     </student>
13
14     <student number="ITCAST_1002">
15         <name>李四</name>
16         <age>25</age>
17         <sex>female</sex>
18     </student>
19 </students>

 

  1. What is DTD: type definition of XML document, constraints of XML.
  2. If the a.xml file is constrained by b.dtd, only the elements and attributes defined in b.dtd can appear in the a.xml file.
  3. HTML is a file that is constrained by DTD, so it is impossible to give elements and attributes at will in the HTML file
  4. Of course, the extension name of dtd is .dtd

File name: student.dtd

1 <!--Hello everyone, this is Comment-->
 2 <!--2015.12.27-->
 3
 4 <!--"#PCDATA" means that the element is nested with a normal text string -->
 5
 6 <!ELEMENT students (student+)>
 7
 8 <!--The "+" plus sign in "student+" indicates that there are one or more student elements -->
 9
10 <!ELEMENT student (name,age,sex)>
11 <!ELEMENT name (#PCDATA)>
12 <!ELEMENT age (#PCDATA)>
13 <!ELEMENT sex (#PCDATA)>

 

DTDs are divided into two categories according to their location:

  • Internal DTD and External DTD.
  • External DTD is divided into: SYSTEM (local) and PUBLIC (online).

 

Internal dtd:

1 <?xml version="1.0" encoding="GB2312" standalone="yes"?>
 2
 3 <!--Hello everyone, I'm a comment-->
 4 <!--2015.12.27-->
 5
 6 <!--"#PCDATA" means that the element is nested with a normal text string -->
 7 <!--The "+" plus sign in "student+" indicates that there are one or more student elements -->
 8 <!DOCTYPE students[
 9 <!ELEMENT students (student+)>
10 <!ELEMENT student (name,age,sex)>
11 <!ELEMENT name (#PCDATA)>
12 <!ELEMENT age (#PCDATA)>
13 <!ELEMENT sex (#PCDATA)>
14 ]>
15
16
17 <students>
18     <student number="ITCAST_1001">
19         <name>张三</name>
20         <age>23</age>
21         <sex>male</sex>
22     </student>
23
24     <student number="ITCAST_1002">
25         <name>李四</name>
26         <age>25</age>
27         <sex>female</sex>
28     </student>
29 </students>

 

Local DTD in External DTD

It is the combination of student.xml and student.dtd above.

Syntax: <! DOCTYPE root element SYSTEM "dtd file path">

 

Online DTD for external DTD

Syntax: <! DOCTYPE root element PUBLIC "dtd name" "dtd url">

1 <?xml version="1.0" encoding="GB2312" standalone="no"?>
 2 <!--Hello everyone, I'm a comment-->
 3 <!--2015.12.27-->
 4
 5 <!DOCTYPE students PUBLIC ".//qdmmy6//DTD ST 1.0//ZH" "http://www.qdmmy6.com/xml/dtds/st.dtd">
 6
 7
 8 <students>
 9     <student number="ITCAST_1001">
10         <name>张三</name>
11         <age>23</age>
12         <sex>male</sex>
13     </student>
14
15     <student number="ITCAST_1002">
16         <name>李四</name>
17         <age>25</age>
18         <sex>female</sex>
19     </student>
20 </students>

 

  1. Definition Elements of DTD Syntax
    1. The syntax for defining elements: <!ELEMENT element name element description>
      • Example 1: <!ELEMENT student (#PCDATA)> , which defines a "student" element whose content is text type ("#PCDATA").
      • Example 2: <!ELEMENT student (name, age, gender)> , defines a "student" element with the contents of "name", "age", and "gender" in sequence.
      • Example 3: <!ELEMENT Student ANY> , which defines a "student" element with unlimited content.
      • Example 4: <!ELEMENT Student EMPTY> , defines a "student" element, which cannot have content, that is, an empty element, and an empty element can have attributes.
    2. The number of occurrences of child elements can be used *, +, ? to specify the number of occurrences of the child element.
      • *: can appear 0~N times;
      • +: can appear 1~N times;
      • ? : Can appear 0~1 times.
      • Example: <!ELEMENT student(name, age?, hobby*, grade+, gender)> , define a "student" element, the first child element is "name", which must appear only once, "age" It is optional, "hobby" can appear 0~N times, grade can appear 1~N times, and "gender" must appear only once.
    3. enum type child element
      • For example: <!ELEMENT student (name|age|gender)>, it means that the "student" sub-element is one of "name", "age" and "gender", and only one of them must be selected.  
  2. Definition properties of DTD syntax
    1. <!ATTLIST> means ATTRIBUTE LIST. (property list)
    2. <!ATTLIST element name attribute name attribute type setting description> define the syntax structure of the attribute
    3. For example: <!ATTLIST student student ID CDATA #REQUIRED> , define the attribute "student ID" for the "student" element, and the type is text, which is required by default.
    4. Description of property settings:
      • #REQUIRED : Specifies that the attribute is required.
      • #IMPLIED : The description attribute is optional.
      • Default value: When no property value is given, the default value is used.
    5. property type
  • CDATA : Text type
  • Enumerated : enumeration type
  • ID: ID type, the ID type attribute is used to mark the uniqueness of the element, that is, the ID attribute value of an element cannot be the same as the ID value of other elements.
  • IDREF: The ID reference type is used to specify another element and establish an association relationship with another element. The attribute value of the IDREF type must be the ID of another element.

 

Personally, I think it may be more intuitive to write dtd in the nested format of xml, for example, the above student.dtd is changed to this

<!ELEMENT students>
  <student+>
    <name>#pcdata</name>
    <age>#pcdata</age>
    <sex>#pcdata</sex>
  </student>
</students>

 It may be easier to read this way, but the native syntax is more convenient to write, after all, dtd files are not for people to see.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326071430&siteId=291194637
DTD