📄 ch14_03.htm
字号:
<?label 14.3. Document Type Definition?><html><head><title>Document Type Definition (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch14_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="ch14_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">14.3. Document Type Definition</h2><p>A <a name="INDEX-2788" /> <a name="INDEX-2,789" /> <a name="INDEX-2,790" />document typedefinition (DTD) tells us how the XML document is structured and whatthe tags mean in relation to one another. Notice that the second linein the quiz XML example contains a document type definition indicatedby a <a name="INDEX-2791" /><a name="INDEX-2792" /><a name="INDEX-2793" /><!DOCTYPE>tag. This tag references a file that contains the DTD for this XMLstructure. Generally, this <!DOCTYPE> tag is used when an<a name="INDEX-2794" />XMLparser wants to validate the XML against a more strict definition.</p><p>For example, the XML shown above could easily be parsed without theDTD. However, the DTD may offer additional hints to the XML parser tofurther validate the document. Here's a sample<em class="filename">quiz.dtd</em><a name="INDEX-2795" /> file:</p><blockquote><pre class="code"><?xml version-"1.0"><!ELEMENT QUIZ (QUESTION*)><!ELEMENT QUESTION (ASK+,CHOICE*,ANSWER+,RESPONSE+)><!ATTLIST QUESTION TYPE CDATA #REQUIRED><!ELEMENT ASK (#PCDATA)><!ELEMENT CHOICE EMPTY> <!ATTLIST CHOICE VALUE CDATA #REQUIRED TEXT CDATA #REQUIRED><!ELEMENT ANSWER (#PCDATA)><!ELEMENT RESPONSE (#PCDATA)> <!ATTLIST RESPONSE VALUE CDATA STATUS CDATA></pre></blockquote><p>The<a name="INDEX-2796" /><a name="INDEX-2797" /><a name="INDEX-2798" /><!ELEMENT>tags describe the actual tags that are valid in the XML document. Inthis case, <QUIZ>, <QUESTION>, <ASK>,<CHOICE>, <ANSWER>, and <RESPONSE> tags areavailable for use in an XML document compliant with the<em class="filename">quiz.dtd</em> file.</p><p>The <a name="INDEX-2799" />parentheses after the name of the elementshow what tags it can contain. The<tt class="literal">*</tt><a name="INDEX-2800" /> symbol is a quantityidentifier. It follows the same basic rules as regular expressionmatching. For example, a <tt class="literal">*</tt> symbol indicates zeroor more of that element is expected to be contained. If we wanted toindicate zero or one, we would have placed a<a name="INDEX-2801" /><tt class="literal">?</tt> in place of the <tt class="literal">*</tt>. Likewise,if we wanted to indicate that one or more of that element has to becontained inside the tag, then we would have used<tt class="literal">+</tt><a name="INDEX-2802" /><a name="INDEX-2803" />.<tt class="literal">#PCDATA</tt><a name="INDEX-2804" /><a name="INDEX-2805" /> <a name="INDEX-2,806" /> is used to indicate that theelement contains character data.</p><p>For this example, the <QUIZ> tag expects to contain zero ormore <tt class="literal">QUESTION</tt> elements while the <QUESTION>tag expects to contain at least one question, answer, and response.Questions can also have zero or more choices. Furthermore, the<tt class="literal">CHOICE</tt> element definition later in the DTD usesthe <tt class="literal">EMPTY</tt><a name="INDEX-2807" /> keyword to indicate that itis a single tag that appears by itself; it does not enclose anything.The <tt class="literal">ASK</tt> element contains character data only.</p><p>After each element is defined, its<a name="INDEX-2808" /><a name="INDEX-2809" />attributes need to be laid out.Questions have a type attribute that takes a string of characterdata. Furthermore, the<tt class="literal">#REQUIRED</tt><a name="INDEX-2810" /> keyword indicates that this datais required in the XML document. The other attribute definitionsfollow a similar pattern in the <em class="filename">quiz.dtd</em> file.</p><p>The <a name="INDEX-2811" />DTD file is optional. You can still<a name="INDEX-2812" />parse an XML document without a documenttype definition. However, with the DTD, the XML parser is providedwith rules that the data<a name="INDEX-2813" />validation should be based on. Maintainingthese validation rules centrally allows the XML format to changewithout having to make as many changes to the parser code. Parsersthat do not use a DTD are called<em class="firstterm">non-validating</em><a name="INDEX-2814" /> XML parsers; the standard<a name="INDEX-2815" /><a name="INDEX-2816" /><a name="INDEX-2817" />Perl module for parsing XML documents,XML::Parser, is a non-validating XML parser.</p><p>Presumably, anybody writing a quiz will use an editor that checkstheir XML against the DTD, or will run the document through avalidating program. Thus, our program will never encounter a questionthat does not contain an answer, or some other violation of the DTD.</p><p>When a program knows the structure of an XML document using a DTD, itcan make other assumptions on how to display that data. For example,a browser could be programmed so that when a quiz document isencountered, it will display the available questions in a list evenif only one question was present in the document itself. Because theDTD tells us that it is possible for many questions to appear in thefile, the browser can determine the context in which to display thedata in the XML document.</p><p>The ability to decouple<a name="INDEX-2818" /><a name="INDEX-2819" />validation rules fromthe parser is especially important on the Web. With the potential formany people to write code that draws information from an XML datasource, any type of mechanism that prevents changes in the XMLdefinition from breaking those parsers will make for a more robustnetwork.</p><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch14_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch14_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">14.2. An Introduction to XML</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">14.4. Writing an XML Parser</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -