📄 0077-0080.html
字号:
<!DOCTYPE HTML PUBLIC "html.dtd"><HTML><HEAD><TITLE>Presenting XML:The XML Approach:EarthWeb Inc.-</TITLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><SCRIPT><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');}//--></SCRIPT></HEAD><BODY BGCOLOR="#FFFFFF" VLINK="#DD0000" TEXT="#000000" LINK="#DD0000" ALINK="#FF0000"><TD WIDTH="540" VALIGN="TOP"><!-- <CENTER><TABLE><TR><TD><FORM METHOD="GET" ACTION="http://search.itknowledge.com/excite/cgi-bin/AT-foldocsearch.cgi"><INPUT NAME="search" SIZE="20" VALUE=""><BR><CENTER><INPUT NAME="searchButton" TYPE="submit" VALUE="Glossary Search"></CENTER><INPUT NAME="source" TYPE="hidden" VALUE="local" CHECKED> <INPUT NAME="bltext" TYPE="hidden" VALUE="Back to Search"><INPUT NAME="sp" TYPE="hidden" VALUE="sp"></FORM></TD><TD><IMG SRC="http://www.itknowledge.com/images/dotclear.gif" WIDTH="15" HEIGHT="1"></TD><TD><FORM METHOD="POST" ACTION="http://search.itknowledge.com/excite/cgi-bin/AT-subscriptionsearch.cgi"><INPUT NAME="search" SIZE="20" VALUE=""><BR><CENTER><INPUT NAME="searchButton" TYPE="submit" VALUE=" Book Search "></CENTER><INPUT NAME="source" TYPE="hidden" VALUE="local" CHECKED> <INPUT NAME="backlink" TYPE="hidden" VALUE="http://search.itknowledge.com:80/excite/AT-subscriptionquery.html"><INPUT NAME="bltext" TYPE="hidden" VALUE="Back to Search"><INPUT NAME="sp" TYPE="hidden" VALUE="sp"></FORM></TD></TR></TABLE></CENTER> --><!-- ISBN=1575213346 //--><!-- TITLE=Presenting XML//--><!-- AUTHOR=Richard Light//--><!-- PUBLISHER=Macmillan Computer Publishing//--><!-- IMPRINT=Sams//--><!-- CHAPTER=05 //--><!-- PAGES=0067-0084 //--><!-- UNASSIGNED1 //--><!-- UNASSIGNED2 //--><P><CENTER><A HREF="0074-0076.html">Previous</A> | <A HREF="../ewtoc.html">Table of Contents</A> | <A HREF="0081-0084.html">Next</A></CENTER></P><A NAME="PAGENUM-77"><P>Page 77</P></A><H3><A NAME="ch05_ 19">Character Data and Markup</A></H3><P>The characters in an XML document represent a mixture ofcharacter data, which is the actual text of the document, andmarkup, which is information that adds value to that text. The start of some markup in an XMLdocument is signaled by one of two special characters: the ampersand (&)and the left angle bracket (<). XML recognizes the following types of markup:</P><UL><LI> Start tags<LI> End tags<LI> Empty elements<LI> Entity references<LI> Character references<LI> Comments<LI> CDATA sections<LI> Document type declarations<LI> Processing instructions</UL><P>In this chapter and the next two chapters, you will learn about thesedifferent types of markup.</P><TABLE BGCOLOR="#FFFF99"><TR><TD>Warning:</TD></TR><TR><TD><BLOCKQUOTE>Because & and < are special characters, you can't use themjust anywhere. They need to be escaped in some way if you want to include themas part of the text of your document. You can use eithercharacter references (such as &#38; and&#60;) or entity references (such as &amp; and&lt;) for & and < respectively. Character references and entity references are describedin Chapter 7.</BLOCKQUOTE></TD></TR></TABLE><P>This is an example of mixed text and markup:</P><!-- CODE SNIP //--><PRE><p>Once more we consider the wise words of L&eacute;ger:<q>Get it right first time!</q></p></PRE><!-- END CODE SNIP //--><P>This example contains the following elements:</P><UL><LI> Start tag:<p> (a new paragraph)<LI> Text: Once more we consider the wise words of L<LI> Entity reference:&eacute; (the letter é)</UL><A NAME="PAGENUM-78"><P>Page 78</P></A><UL><LI> Text:ger:<LI> Start tag:<q> (a quotation within this paragraph)<LI> Text: Get it right first time!<LI> End tag:</q> (end the quotation)<LI> End tag:</p> (end the paragraph)</UL><P>The general rule is that if you strip out the markup, you are left with thecharacter data. This also applies within elements: The character data withinan element is all the characters between its start tag and end tag that arenot markup. Thus, in the preceding example, the<P> element contains the following character data:</P><!-- CODE SNIP //--><PRE>Once more we consider the wise words of Lger:Get it right first time!</PRE><!-- END CODE SNIP //--><P>The <Q> element contains the following character data:</P><!-- CODE SNIP //--><PRE>Get it right first time!</PRE><!-- END CODE SNIP //--><TABLE BGCOLOR="#FFFF99"><TR><TD>Note:</TD></TR><TR><TD><BLOCKQUOTE>The quoted phrase is part of the character data ofboth elements.</BLOCKQUOTE></TD></TR></TABLE><TABLE BGCOLOR="#FFFF99"><TR><TD>Note:</TD></TR><TR><TD><BLOCKQUOTE>The accented character appears to have vanished. This happensbecause it is represented by markup—an entity reference—even though it is onlya single character. Obviously, this character will appear correctly asé when the XML document is viewed or printed, but within the document it maintainsits separate status. The conversion of&eacute; to é happens on the client's machine, which enables XML to represent accented characters correctly ona wide range of platforms despite the fact that they use differentencodings.</BLOCKQUOTE></TD></TR></TABLE><H3><A NAME="ch05_ 20">CDATA Sections</A></H3><P>Sometimes it is really tedious to have to escape every& and < character that crops up in your text. What if you are trying (as I am now) to write abook that explains XML and gives lots of examples? Each example will belittered with special characters. XML providesCDATA sections to get around this problem. As with comments,& and < can be entered freely within CDATA sections.</P><A NAME="PAGENUM-79"><P>Page 79</P></A><P>CDATA sections can occur anywhere that character data might occur. Theyare used to escape blocks of text that would otherwise be interpreted asmarkup. CDATA sections begin with the sequence<![CDATA[ and end with the sequence ]]>, as in the following example:</P><!-- CODE SNIP //--><PRE><![CDATA[ <p>Once more we consider the wise words of <BR>L&eacute;ger: ]]></PRE><!-- END CODE SNIP //--><P>This is how the first line of the previous markup example was encoded forthis book.</P><P>In the special case of CDATA sections, characterdata is any string of characters, not including the sequence]]>, that terminates the CDATA section.</P><P>Note that CDATA sections cannot occur inside otherCDATA sections; that is, they cannot nest.</P><H3><A NAME="ch05_ 21">White Space Handling</A></H3><P>As an aid to greater readability, you can insertwhite space into XML documents. XML defines white space as any combination of the following:</P><UL><LI> The space character<LI> Carriage return<LI> Line feed<LI> Tab</UL><P>For example, you might indent elements to show their hierarchical structure:</P><!-- CODE //--><PRE><p>Things I would like for my birthday: <list> <item>a watch</item> <item>socks</item> </list></p></PRE><!-- END CODE //--><P>Here new lines and tabs have been used to indent the markup and give aclearer visual layout, even though they are not strictly required. Without theextraneous white space this example would look as follows:</P><!-- CODE SNIP //--><PRE><p>Things I would like for my birthday:<list><item>a watch</item><item>socks</item></list></p></PRE><!-- END CODE SNIP //--><P>This is much less clear to a human reader.</P><A NAME="PAGENUM-80"><P>Page 80</P></A><P>This extra white space is treated as part of the document and is passed toan application by the XML processor. However, in the following special cases,an XML processor will flag the white space that occurs immediately before orafter markup as not significant:</P><UL><LI> When the XML processor is reading the DTD (as discussed inChapter 8, "Keeping It Tidy: The XML Rule Book")<LI> When the current element has element content rather thanmixed content (as discussed in the "Element Declaration" section of<LI>Chapter 8)</UL><P>In the previous example, you can reasonably assume that the contentmodel for <P> elements allows mixed content, that the content model for<list> allows only <item>, and that the content model for<item> allows mixed content (or just text, which amounts to the same thing). Then an XMLprocessor would mark as non-significant the extra white space within the<list> element, which is the only element type in this example that has element content.An application might then choose to treat this piece of markup as though thewhite space within <list> did not exist:</P><!-- CODE SNIP //--><PRE><p>Things I would like for my birthday: <list><item>a watch</item><item>socks</item></list></p></PRE><!-- END CODE SNIP //--><P>In cases when these rules might lead to important spaces being ignored byan application (for example, those in poetry or source code), XML providesa mechanism for signaling to an application that this white space should betreated as significant. The XML-SPACE attribute can be added to any element'sattribute list. Its declaration takes the following form:</P><!-- CODE SNIP //--><PRE>XML-SPACE (DEFAULT | PRESERVE) #IMPLIED</PRE><!-- END CODE SNIP //--><P>Specifying this attribute with the value"PRESERVE" indicates that white space is to be treated as significant within thiselement:</P><!-- CODE //--><PRE><POEM><L XML-SPACE="PRESERVE">One</L><L XML-SPACE="PRESERVE"> Two</L><L XML-SPACE="PRESERVE"> Three</L></POEM></PRE><!-- END CODE //--><P><CENTER><A HREF="0074-0076.html">Previous</A> | <A HREF="../ewtoc.html">Table of Contents</A> | <A HREF="0081-0084.html">Next</A></CENTER></P></TD></TR></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -