DTD and XML basic Syntax rules

Source: Internet
Author: User

a DTD (document type definition) can define a legitimate XML document structure that uses a series of legitimate elements to define the structure of a document. DTDs are divided into internal DTDs and external DTDs, the so-called internal DTD refers to the DTD within a document and is used only by that document. An external DTD means that the DTD is not inside the document and can be shared by all other documents. The relationship between a DTD document and an instance of an XML document can be seen as a relationship between classes and objects.

(1) Compilation and reference of external DTD files

Create a new external family.dtd file

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/03/wKiom1cPjB2hdvBCAABtiyIH23o564.png "title=" Qq20160414202125.png "alt=" Wkiom1cpjb2hdvbcaabtiyih23o564.png "/>

<! ELEMENT family (father,mother,son+) ><! ELEMENT father (Name,age) ><! ELEMENT name (#PCDATA) ><! ELEMENT Age (#PCDATA) ><! ELEMENT mother (Name,age) ><! ELEMENT Son (Name|wife|grandson) *><! ELEMENT Wife (#PCDATA |name) *><! ELEMENT grandson (name) ><! attlist son status CDATA #REQUIRED >

Writing an XML document referencing a DTD file family.dtd

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/03/wKiom1cPkbzinKnfAABUJPAfxvc325.png "title=" Qq20160414202212.png "alt=" Wkiom1cpkbzinknfaabujpafxvc325.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/03/wKiom1cPkqSgi2lJAAAo_g1x0ps355.png "title=" Picture 2.png "Width=" "height=" 329 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:329PX; "alt=" Wkiom1cpkqsgi2ljaaao_g1x0ps355.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/00/wKioL1cPk3-QdoAEAAA9PlyFz7c626.png "title=" Picture 3.png "Width=" "height=" 329 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:329PX; "alt=" Wkiol1cpk3-qdoaeaaa9plyfz7c626.png "/>

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE family SYSTEM "file:///E:/Users/Little Love Love/desktop/family.dtd" ><family><father><name> Zhang San < /name> <age>40</age></father><mother><name> Liu Li </name><age>36</ Age></mother><son status= "Bigson" ><name> Zhang Tianshi </name><wife><name> Wang Hua </name ></wife><grandson><name> Zhang Xiaohua </name></grandson></son><son status= " Smallson "><name> Zhang Shuai </name></son></family>

(2) Internal DTD file

Create a new XML file with an internal DTD file class.xml

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/03/wKiom1cPkmfweAkDAABb89CctMg019.png "title=" Qq20160414202145.png "alt=" Wkiom1cpkmfweakdaabb89cctmg019.png "/>

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE class[<! ELEMENT Class (student*) ><! ELEMENT Student (Name,age,sex) ><! ELEMENT name (#PCDATA) ><! ELEMENT Age (#PCDATA) ><! ELEMENT Sex (#PCDATA) ><! Attlist class ClassId ID #REQUIRED ><! attlist student Stuid IDREF #IMPLIED >]><class classid= "A01" ><student stuid= "A01" ><name> Zhang San </name><age>18</age><sex> male </sex></student><student stuid= "A01" >< name> John Doe </name><age>18</age><sex> Women </sex></student><student>< Name> Harry </name><age>17</age><sex> Women </sex></student></class>

As you can see from the XML file above

#IMPLIED: Indicates that the attribute can or may not be.

#REQUIRED: Indicates that the attribute must have.

There is also the characteristic of a property

#FIXED: Indicates that the property's property value is fixed.

Attribute values that are declared to be of the IDREF type must reference the property value of the ID property type.

(3) Any of the usage rules

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE company[<! ELEMENT company any><! ELEMENT worker (Name,sex,age) ><! ELEMENT name (#PCDATA) ><! ELEMENT Sex (#PCDATA) ><! ELEMENT Age (#PCDATA) ><! Attlist worker WorkID ID #REQUIRED >]><company><worker workid= "A10" ><name>zhangsan</name ><sex>nv</sex><age>28</age></worker><company></company><name >lisi</name><age>30</age><name>wangwu</name><sex>nan</sex></ Company>

The any type means that it can contain multiple child elements, is unrestricted, and can contain string data. However, any is generally used in the declaration of the root element.

(4) NmToken and Nmtokens

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE student[<! ELEMENT Student (#PCDATA) ><! attlist student Stuid NmToken #REQUIRED ><! attlist student classId nmtokens #REQUIRED >]><student stuid= "A10" classid= "A10" ></student>

The above code can be compiled by compiling the error message that occurs when the XML is the following code

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE student[<! ELEMENT Student (#PCDATA) ><! attlist student Stuid NmToken #REQUIRED ><! attlist student classId nmtokens #REQUIRED >]><student stuid= "A" classid= "A10" ></student>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/04/wKiom1cPnqCSXSWqAAAeZfWcAIA422.png "title=" Qq20160414214217.png "alt=" Wkiom1cpnqcsxswqaaaezfwcaia422.png "/>

The nmtoken type requires that the attribute value cannot contain spaces and must begin with an underscore, a number, or a letter.

The nmtokens type is similar to the NmToken class , and thenmtokens type requires that the attribute value must begin with an underscore, a number, or a letter. But it can contain spaces.

(5) Enum type

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE people[<! ELEMENT people (person+) ><! ELEMENT person (#PCDATA) ><! attlist person Sex (male | female) #IMPLIED > <! Attlist Person Education (High School | university | postgraduate | phd) #IMPLIED >]><people><person sex = "female" education = "University" >lily</person>< Person ></person></people>

Lists the items to select, similar to the drop-down list.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/04/wKiom1cPodbTdqjkAAAmN8LgV1s867.png "title=" Qq20160414215627.png "alt=" Wkiom1cpodbtdqjkaaamn8lgv1s867.png "/>

DTD and XML basic Syntax rules

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.