📄 ch02_08.htm
字号:
<html><head><title>Processing Instructions and Other Markup (Perl and XML)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Erik T. Ray and Jason McIntosh" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="059600205XL" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl and XML" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl & XML" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_07.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch02_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.8. Processing Instructions and Other Markup</h2><p>Besides elements, you can use several other syntactic objects to makeXML easier to manage. <em class="emphasis">Processinginstructions</em><a name="INDEX-126" /><a name="INDEX-127" /> (PIs) are used to convey informationto a particular XML processor. They specify the intended processorwith a<a name="INDEX-128" /><em class="emphasis">target</em> parameter, which is followed by anoptional<a name="INDEX-129" /><em class="emphasis">data</em> parameter. Any program thatdoesn't recognize the target simply skips the PI andpretends it never existed. Here is an example based on an actualbehind-the-scenes O'Reilly book hacking experience:</p><blockquote><pre class="code"><?file-breaker start chap04.xml?><chapter><title>The very long title<?lb?>that seemed to go on forever and ever</title><?xml2pdf vspace 10pt?></pre></blockquote><p>The first PI has a target called <tt class="literal">file-breaker</tt> andits data is <tt class="literal">chap04.xml</tt>. A program reading thisdocument will look for a PI with that target keyword and will act onthat data. In this case, the goal is to create a new file and savethe following XML into it.</p><p>The second PI has only a target, <tt class="literal">lb</tt>. We haveactually seen this example used in documents to tell an XML processorto create a line break at that point. This example has two problems.First, the PI is a replacement for a space character;that's bad because any program thatdoesn't recognize the PI will not know that a spaceshould be between the two words. It would be better to place a spaceafter the PI and let the target processor remove any following spaceitself. Second, the target is an instruction, not an actual name of aprogram. A more unique name like the one in the next PI,<tt class="literal">xml2pdf</tt>, would be better (with the<tt class="literal">lb</tt> appearing as data instead).</p><p>PIs are convenient for developers. They have no solid rules thatspecify how to name a target or what kind of data to use, but ingeneral, target names ought to be very specific and data should bevery short.</p><p>Those who have written documents using Perl'sbuilt-in Plain Old Documentation mini-markup language<a href="#FOOTNOTE-9">[9]</a> hackers maynote a similarity between PIs and certain <a name="INDEX-130" />POD directives,particularly the <tt class="literal">=for</tt> paragraphs and<tt class="literal">=begin</tt>/<tt class="literal">=end</tt> blocks. In theseparagraphs and blocks, you can leave little messages for a PODprocessor with a target and some arguments (or any string of text).</p><blockquote class="footnote"><a name="FOOTNOTE-9" /><p>[9]The gory details of which lie in Chapter 26 of<em class="citetitle">Programming Perl, Third Edition</em> or in the<tt class="literal">perlpod</tt> manpage.</p> </blockquote><p>Another useful markup object is the <em class="emphasis">XMLcomment</em><a name="INDEX-131" />. Comments are regions of text that any XMLprocessor ignores. They are meant to hold information for human eyesonly, such as notes written by authors to themselves and theircollaborators. They are also useful for turning"off" regions ofmarkup -- perhaps if you want to debug the document oryou're afraid to delete something altogether.Here's an example:</p><blockquote><pre class="code"><!-- this is invisible to the parser -->This is perfectly visible XML content.<!-- <para>This paragraph is no longer part of the document.</para>--></pre></blockquote><p>Note that these comments look and work exactly like their HTMLcounterparts.</p><p>The only thing you can't put inside a comment isanother comment. You can't even feint at nestingcomments; the string " -- ", forexample, is illegal in a comment, no matter how you use it.</p><p>The last syntactic convenience we will discuss is the<em class="emphasis">CDATA section</em><a name="INDEX-132" />. CDATA stands forcharacter data, which in XML parlance means unparsed content. Inother words, the XML processor treats an entire CDATA section asthough it contains no markup at all -- even things that look likemarkup. This is useful if you want to include a large region ofillegal characters like <tt class="literal"><</tt>,<tt class="literal">></tt>, and <tt class="literal">&</tt> that would bedifficult to convert into character entity references.</p><p>For example: </p><blockquote><pre class="code"><codelisting><![CDATA[if( $val > 3 && @lines ) { $input = <FILE>;}]]></codelisting></pre></blockquote><p>Everything after <tt class="literal"><![CDATA[</tt> and before the<tt class="literal">]]></tt> is treated as nonmarkup data, so the markupsymbols are perfectly fine. We rarely use CDATA sections because theyare kind of unsightly, in our humble opinion, and make writing XMLprocessing code a little harder. But it's there ifyou need it.<a href="#FOOTNOTE-10">[10]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-10" /><p>[10]We use CDATA throughout theDocBook-flavored XML that makes up this book. We wrapped all the codelistings and sample XML documents in it so we didn'thave to suffer the bother of escaping every <tt class="literal"><</tt>and <tt class="literal">&</tt> that appears in them.</p> </blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_07.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch02_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.7. The XML Declaration</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">2.9. Free-Form XML and Well-Formed Documents</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -