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

📄 ch02_09.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>Free-Form XML and Well-Formed Documents (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 &amp; 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 &amp; 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_08.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_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.9. Free-Form XML and Well-Formed Documents</h2><p>XML's grandfather, SGML, required that every elementand attribute be documented thoroughly with a long list ofdeclarations in the DTD. We'll describe what we meanby that thorough documentation in the next section, but for now,imagine it as a blueprint for a document. This blueprint addsconsiderable overhead to the processing of a document and was aserious obstacle to SGML's status as a popularmarkup language for the Internet. HTML, which was originallydeveloped as an SGML instance, was hobbled by this enforcedstructure, since any "valid" HTMLdocument had to conform to the HTML DTD. Hence, extending thelanguage was impossible without approval by a web committee.</p><p>XML does away with that requirement by allowing a special conditioncalled <em class="emphasis">free-form</em><a name="INDEX-133" /> XML. In this mode, a document has to followonly minimal syntax rules to be acceptable. If it follows thoserules, the document is<a name="INDEX-134" /> <em class="emphasis">well-formed</em>.Following these rules is wonderfully liberating for a developerbecause it means that you don't have to scan a DTDevery time you want to process a piece of XML. All a processor has todo is make sure that minimal syntax rules are followed.</p><p>In free-form XML, you can choose the name of any element. Itdoesn't have to belong to a sanctioned vocabulary,as is the case with HTML. Including frivolous markup into yourprogram is a risk, but as long as you know whatyou're doing, it's okay. If youdon't trust the markup to fit a patternyou're looking for, then you need to use element andattribute declarations, as we describe in the next section.</p><p>What are these rules? Here's a short list as seenthough a coarse-grained spyglass:</p><ul><li><p>A document can have only one top-level element, the<em class="emphasis">document element</em><a name="INDEX-135" />, that contains all the other elementsand data. This element does not include the XML declaration anddocument type declaration, which must precede it.</p></li><li><p>Every element with content must have both a start tag and an end tag. </p></li><li><p>Element and attribute names are case sensitive, and only certaincharacters can be used (letters, underscores, hyphens, periods, andnumbers), with only letters and underscores eligible as the firstcharacter. Colons are allowed, but only as part of a declarednamespace prefix.</p></li><li><p>All attributes must have values and all attribute values must bequoted.</p></li><li><p>Elements may never overlap; an element's start andend tags must both appear within the same element.</p></li><li><p>Certain characters, including angle brackets(<tt class="literal">&lt;</tt><a name="INDEX-136" /> <a name="INDEX-137" /> <tt class="literal">&gt;</tt>) and theampersand (<tt class="literal">&amp;</tt>) are reserved for markup and arenot allowed in parsed content. Use character entity referencesinstead, or just stick the offending content into a CDATA section.</p></li><li><p>Empty elements must use a syntax distinguishing them from nonemptyelement start tags. The syntax requires a <a name="INDEX-138" /> <a name="INDEX-139" />slash (<tt class="literal">/</tt>) beforethe closing bracket (<tt class="literal">&gt;</tt>) of the tag.</p></li></ul><p>You will encounter more rules, so for a more complete understandingof well-formedness, you should either read an introductory book onXML or look at the W3C's official recommendation at<a href="http://www.w3.org/XML">http://www.w3.org/XML</a>.</p><p>If you want to be able to process your document with XML-usingprograms, make sure it is always well formed. (After all,there's no such thing as non-well-formed XML.) Atool often used to check this status is called a<em class="emphasis">well-formednesschecker</em><a name="INDEX-140" />, whichis a type of XML<a name="INDEX-141" />parser that reports errors to theuser. Often, such a tool can be detailed in its analysis and give youthe exact line number in a file where the problem occurs.We'll discuss checkers and parsers in <a href="ch03_01.htm">Chapter 3, "XML Basics: Reading and Writing"</a>. </p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_08.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_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.8. Processing Instructions and Other Markup</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.10. Declaring Elements and Attributes</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 &copy; 2002</a> O'Reilly &amp; 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 + -