Dtd
Basic concept: DTD (document type definition), which is typically used in conjunction with XML files, is primarily useful for constraining XML.
In addition to DTD technology, there is a schema technique that can also be used to constrain the writing specification of XML files.
Now take a look at the question:
<StuID= "a"0'0'1<" ><name>Yang too</name><Sex>Man</Sex>< Age>30</ Age><Introduction>I'm a good guy.</Introduction><Area>100 flat</Area></Stu>
XML is too free.
Quick Start case
The basic syntax is:
<! Element name Type >
<?XML version= "1.0" encoding= "Utf-8"?><!--introduce a DTD to constrain the XML file -<!DOCTYPE class SYSTEM "Myclass2.dtd"><class> <Student> <name>Stephen chow</name> <Age>23</Age> <Introduction>Study hard</Introduction> </Student> <Student> <name>Brigitte</name> <Age>32</Age> <Introduction>is a good student</Introduction> </Student></class>
MYCLASS2.DTD <! ELEMENT Class (student +) > <! ELEMENT Student (name, age, Introduction) > <! ELEMENT name (#PCDATA) > <! ELEMENT Age (#PCDATA) > <! ELEMENT Introduction (#PCDATA) >
HTML writing to complete the validation
<HTML><Head><!--Write a simple parsing tool to parse whether the XML DTD is compatible -<Scriptlanguage= "JavaScript"><!-- varxmldoc= NewActiveXObject ("Microsoft.XMLDOM"); Xmldoc.validateonparse= "true";//turn on the checksumXmldoc.load ("Myclass2.xml");//specify which XML file to validateDocument.writeln ("the error message is:"+Xmldoc.parseError.reason+"<br/>"); Document.writeln ("the wrong line is:"+xmldoc.parseError.line);// -</Script></Head><Body></Body></HTML>
Details of the DTD
(1) Classification of DTDs
Internal DTD
External DTD
Internal DTD document: <! DOCTYPE root element [ definition content ]>
External DTD document: <! DOCTYPE root element SYSTEM "DTD file path " >
(2) There are two ways to introduce a DTD in XML
- Introducing a local DTD
<! DOCTYPE root element SYSTEM ' address ' >
- Introducing a public DTD
<! DOCTYPE root element Public ' address ' >
(2) <! Element name Type >
Type: EMPTY, any, #PCDATA
(3) modifier for DTD
(4) Details of attributes
Basic syntax
<! attlist element Name property name Type features ..... >
There are five types of L:
CDATA means that you can put text
The ID represents the value of the property, cannot be duplicated, and does not start with a number.
Idref/idrefs when a property value of an element needs to refer to another ID, use IDREF, and if you want to refer to more than one, use IDREFS, separated by a space.
enumerated represents the value of a property, but only for example
<! attlist student Address CDATA #FIXED "Beijing" School ID #REQUIRED eldest brother IDREFS #REQUIRED
After ENTITY
There are four characteristics of attributes
#REQUIRED indicates that there must be
#IMPLIED indicate that there can be
#FIXED "Value" indicates what must be if there is one
The default "value" means that it defaults if not specified.
entities (Entity)
Is that the entity is used to create an alias for a piece of content, which can then be referenced in an XML document by using an alias.
Java:
String str= "Hello";
Define STR, and elsewhere, we can use str to access ' Hello '
(1) Classification
1, referencing entities
Case
Defined in the DTD:
<! ENTITY mycopy "My company Copyright" >
Description: It is best to place the definition at the end of the DTD
Using in XML
&mycopy;
2, parameter entity
Basic syntax
<! entities% entity name "entity content">
Reference
% entity name;
Example:
<!ELEMENT Class (student *)><!ENTITY% myname "name"><!ELEMENT Student (%myname;, introduction, age)><!attlist student Address CDATA #FIXED "Beijing" School ID #REQUIRED eldest brother IDREFS #REQUIRED sex (male | female) #REQUIRED> <!ELEMENT%myname; (#PCDATA)><!ELEMENT Age (#PCDATA)><!ELEMENT Introduction (#PCDATA)><!ENTITY mycopy "My company Copyright">
The goal of learning DTDs: Companies rarely let programmers write DTDs themselves, requiring programmers to read DTDs, and to write corresponding XML based on the DTD given
Actual case:
A Product Catalog
DTD file:
<! ENTITY AUTHOR "John Doe" ><! ENTITY Company "JD Power Tools, Inc." ><! ENTITY EMAIL "[email protected]" > <! ELEMENT CATALOG (product+) > <! ELEMENT PRODUCT (specifications+,options?,price+,notes?) ><! Attlist PRODUCTNAME CDATA #IMPLIEDCATEGORY (handtool| table| shop-professional) "Handtool" PARTNUM CDATA #IMPLIEDPLANT (pittsburgh| milwaukee| Chicago) "Chicago" INVENTORY (instock| backordered| Discontinued) "Instock" > <! ELEMENT Specifications (#PCDATA) ><! Attlist specificationsweight CDATA #IMPLIEDPOWER CDATA #IMPLIED > <! ELEMENT OPTIONS (#PCDATA) ><! Attlist Optionsfinish (metal| polished| Matte) "Matte" ADAPTER (included| optional| notapplicable) "Included" Case (hardshell| Soft| notapplicable) "Hardshell" > <! ELEMENT Price (#PCDATA) ><! Attlist PRICEMSRP CDATA #IMPLIEDWHOLESALE CDATA #IMPLIEDSTREET CDATA #IMPLIEDSHIPPING CDATA #IMPLIED > <! ELEMENT NOTES (#PCDATA);
XML Authoring Case:
<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE CATALOG SYSTEM ' product.dtd '><CATALOG><PRODUCTNAME= "Master Kang's mineral water"CATEGORY= "Handtool"PARTNUM= "ABC"PLANT= "Milwaukee"INVENTORY= "Backordered"><SpecificationsWEIGHT= "$"POWER= "All" >Here's the details.</Specifications>< Price>110</ Price></PRODUCT></CATALOG>
CDATA means character data (character)
PCDATA means the parsed character data (parsed character).
PDATA is the text that will be parsed by the parser. The text will be examined by the parser entity and tagged.
Introduction to the DTD document type definition file