⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch14_02.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
字号:
<?label 14.2. An Introduction to XML?><html><head><title>An Introduction to XML (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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">14.2. An Introduction to XML</h2><p><a name="INDEX-2763" />XML is useful because it provides anindustry standard way of describing data. In addition, XMLaccomplishes this feat in a style similar to HTML, which thousands ofdevelopers are already familiar with. CGI programs that speak XMLwill be able to deliver to and retrieve data from any XML-compliantPerl script or Java applet.</p><p>It is possible to use CGI as middleware without a data descriptionlanguage such as XML. The success of libraries such as LWP for Perldemonstrates this. However, most web pages still deliver data asplain <a name="INDEX-2764" />HTML. Using LWP to grab these pages andthe HTML::Parser to<a name="INDEX-2765" />parse them leaves much to bedesired. Although HTML has to be produced in order for a web browserto consume the data even when XML is used, the HTML itself is likelyto change depending on how the web designer wants the page to look,even if the data described in XML would still remain the same. Forthis reason, writing a parser for an HTML document can be problematicbecause the HTML parser will break as soon as the structure of howthe data is displayed is changed.</p><p>On the client side of the coin, those projects requiring thesophisticated data-display capabilities of Java need to have some wayof obtaining their data. Enabling <a name="INDEX-2766" />Java applets to talk toCGI programs provides a lightweight and easy way to gather the datafor presentation.</p><p>For the most part, HTML has served its purpose well. Web browsershave successfully dealt with HTML markup tags to display content tousers for years. However, while human readers can absorb the data inthe context of their own language,<a name="INDEX-2767" />machines find it difficult to interpretthe ambiguity of data written in a natural language such as Englishinside an HTML document. This problem brought about the recognitionthat what the Web needs is a language that could mark up content in away that is easily machine-readable.</p><p><a name="INDEX-2768" />XML was designed to make up for manyof HTML's limitations in this area. The following is a list offeatures XML provides that makes it useful as a mechanism fortransporting data from program to program:</p><ol><li><p>New<a name="INDEX-2769" />tags and tag hierarchies can bedefined to represent data specific to your application. For instance,a quiz can contain &lt;QUESTION&gt; and &lt;ANSWER&gt; tags.</p></li><li><p><a name="INDEX-2770" /><a name="INDEX-2771" />Documenttype definitions can be defined for data validation. You can require,for instance, that every &lt;QUESTION&gt; be associated with exactlyone &lt;ANSWER&gt;.</p></li><li><p>Data transport is <a name="INDEX-2772" />Unicode-compliant, which isimportant for non-ASCII character sets.</p></li><li><p>Data is provided in a way that makes it easily transportable via<a name="INDEX-2773" />HTTP.</p></li><li><p>Syntax is simple, allowing<a name="INDEX-2774" /><a name="INDEX-2775" />parsers to be simple.</p></li></ol><p>As an example, let's look at a sample <a name="INDEX-2776" /> <a name="INDEX-2,777" />XML document that mightcontain the data for an online quiz. At the most superficial level, aquiz has to be represented as a collection of questions and theiranswers. The XML looks like this:</p><blockquote><pre class="code">&lt;?xml version="1.0"?&gt;&lt;!DOCTYPE quiz SYSTEM "quiz.dtd"&gt;&lt;QUIZ&gt;  &lt;QUESTION TYPE="Multiple"&gt;    &lt;ASK&gt;      All of the following players won the regular season MVP and playoff      MVP in the same year, except for:    &lt;/ASK&gt;    &lt;CHOICE VALUE="A" TEXT="Larry Bird"/&gt;    &lt;CHOICE VALUE="B" TEXT="Jerry West"/&gt;    &lt;CHOICE VALUE="C" TEXT="Earvin Magic Johnson"/&gt;    &lt;CHOICE VALUE="D" TEXT="Hakeem Olajuwon"/&gt;    &lt;CHOICE VALUE="E" TEXT="Michael Jordan"/&gt;        &lt;ANSWER&gt;B&lt;/ANSWER&gt;    &lt;RESPONSE VALUE="B"&gt;      West was awesome, but they did not have a playoff       MVP in his day.    &lt;/RESPONSE&gt;    &lt;RESPONSE STATUS="WRONG"&gt;      How could you choose Bird, Magic, Michael, or Hakeem?    &lt;/RESPONSE&gt;  &lt;/QUESTION&gt;    &lt;QUESTION TYPE="Text"&gt;    &lt;ASK&gt;      Who is the only NBA player to get a triple-double by halftime?    &lt;/ASK&gt;        &lt;ANSWER&gt;Larry Bird&lt;/ANSWER&gt;     &lt;RESPONSE VALUE="Larry Bird"&gt;       You got it! He was quite awesome!     &lt;/RESPONSE&gt;     &lt;RESPONSE VALUE="Magic Johnson"&gt;       Sorry. Magic was just as awesome as Larry, but he never got a       triple-double by halftime.     &lt;/RESPONSE&gt;     &lt;RESPONSE STATUS="WRONG"&gt;       I guess you are not a Celtics Fan.     &lt;/RESPONSE&gt;  &lt;/QUESTION&gt;&lt;/QUIZ&gt;</pre></blockquote><p>You can see from the above document that XML is actually very simple,and it is very similar to <a name="INDEX-2778" />HTML. This is no accident. One ofXML's primary design goals is to make it compatible with theInternet. The other major goal is to make the language so simple thatit is relatively trivial to write an XML parser.</p><p>From the structure in the sample XML document, you can ascertain thatthe <a name="INDEX-2779" />root data structure is a quizsurrounded by &lt;QUIZ&gt; tags. All XML documents must present thedata with at least one root structure surrounding the whole document.</p><p>Within the quiz structure shown here, there are two questions. Withinthose questions are descriptions of the question itself, an answer tothe question, and a host of possible responses.</p><p>Obviously, this input has to be accompanied by a<a name="INDEX-2780" />style sheet or someother guide to the browser, so that the browser knows basic thingslike not displaying the answers with the questions. Later in thischapter, we will write a Perl program to translate an XML documentinto standard HTML.</p><p>The question tags <a name="INDEX-2781" /><a name="INDEX-2782" /> <a name="INDEX-2,783" />are written with an<a name="INDEX-2784" /> <a name="INDEX-2,785" /> <a name="INDEX-2,786" />open and closing tag to illustrate thatmultiple datasets (ask, answer, response) are placed between them. Onthe other hand, we made the choices for a multiple-choice questioninto single, empty tags. XML makes this clear by forcing a"/" at the end of the single tag definition.</p><p>This is one of the main areas where XML differs from HTML. HTML wouldjust leave the single empty tag as is. However, the designers of XMLfelt that it was easier to write a parser if that parser knew that itdid not have to look for a closing tag to accommodate the start tagas soon as it realized the single tag ends with a "/&gt;"instead of "&gt;" by itself.</p><p>The above XML document is arbitrarily structured. We could havepresented the information in different ways.</p><p>For example, we could have made the &lt;CHOICE&gt; tag open insteadof empty so that a choice could handle more definitions inside ofitself. Using an open tag would allow a round-robin list of possiblechoices to present so the choices do not appear the same all thetime. This is an important XML point: XML was designed to accommodateany data <a name="INDEX-2787" />structure.</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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">14. Middleware and 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.3. Document Type Definition</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 &copy; 2001</a> O'Reilly &amp; 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 + -