📄 introxml4.html
字号:
<div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version='1.0' encoding='utf-8'?><!-- A SAMPLE set of slides --><a name="wp67470"> </a><slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly" > <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide> <!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item> <item/> <item>Who <em>buys</em> WonderWidgets</item> </slide</slideshow><a name="wp67471"> </a></pre></div><a name="wp67923"> </a><p class="pBody">Save a copy of this file as <code class="cCode">slideSample01.xml</code>, so you can use it as the initial data structure when experimenting with XML programming operations. </p><a name="wp67861"> </a><h3 class="pHeading2">Writing Processing Instructions</h3><a name="wp67862"> </a><p class="pBody">It sometimes makes sense to code application-specific processing instructions in the XML data. In this exercise, you'll add a processing instruction to your <code class="cCode">slideSample.xml</code> file.</p><hr><a name="wp67864"> </a><p class="pNote">Note: The file you'll create in this section is <code class="cCode"><a href="../examples/xml/samples/slideSample02.xml" target="_blank">slideSample02.xml</a></code>. (The browsable version is <code class="cCode"><a href="../examples/xml/samples/slideSample02-xml.html" target="_blank">slideSample02-xml.html</a></code>.) </p><hr><a name="wp67872"> </a><p class="pBody">As you saw in <a href="IntroXML2.html#wp63939">Processing Instructions</a>, the format for a processing instruction is <code class="cCode"><?target data?></code>, where "target" is the target application that is expected to do the processing, and "data" is the instruction or information for it to process. Add the text highlighted below to add a processing instruction for a mythical slide presentation program that will query the user to find out which slides to display (technical, executive-level, or all):</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><slideshow ... ><a name="wp67873"> </a> <code class="cCodeBold"><!-- PROCESSING INSTRUCTION --> <?my.presentation.Program QUERY="exec, tech, all"?></code><a name="wp67874"> </a> <!-- TITLE SLIDE --><a name="wp67875"> </a></pre></div><a name="wp67876"> </a><p class="pBody">Notes:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp67877"> </a><div class="pSmartList1"><li>The "data" portion of the processing instruction can contain spaces, or may even be null. But there cannot be any space between the initial <code class="cCode"><?</code> and the target identifier.</li></div><a name="wp67878"> </a><div class="pSmartList1"><li>The data begins after the first space.</li></div><a name="wp67879"> </a><div class="pSmartList1"><li>Fully qualifying the target with the complete Web-unique package prefix makes sense, so as to preclude any conflict with other programs that might process the same data.</li></div><a name="wp67880"> </a><div class="pSmartList1"><li> For readability, it seems like a good idea to include a colon (:) after the name of the application, like this:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?my.presentation.Program: QUERY="..."?><a name="wp67881"> </a></pre></div></ul></div><a name="wp67882"> </a><p class="pDefinition">The colon makes the target name into a kind of "label" that identifies the intended recipient of the instruction. However, while the w3c spec allows ":" in a target name, some versions of IE5 consider it an error. For this tutorial, then, we avoid using a colon in the target name.</p><a name="wp67940"> </a><p class="pBody">Save a copy of this file as <code class="cCode">slideSample02.xml</code>, so you can use it when experimenting with processing instructions.</p><a name="wp69037"> </a><h3 class="pHeading2">Introducing an Error</h3><a name="wp69038"> </a><p class="pBody">The parser can generate one of three kinds of errors: fatal error, error, and warning. In this exercise, you'll make a simple modification to the XML file to introduce a fatal error. Then you'll see how it's handled in the Echo app.</p><hr><a name="wp69040"> </a><p class="pNote">Note: The XML structure you'll create in this exercise is in <code class="cCode"><a href="../examples/xml/samples/slideSampleBad1.xml" target="_blank">slideSampleBad1.xml</a></code>. (The browsable version is <code class="cCode"><a href="../examples/xml/samples/slideSampleBad1-xml.html" target="_blank">slideSampleBad1-xml.html.</a></code>)</p><hr><a name="wp69044"> </a><p class="pBody">One easy way to introduce a fatal error is to remove the final <code class="cCode">"/"</code> from the empty <code class="cCode">item</code> element to create a tag that does not have a corresponding end tag. That constitutes a fatal error, because all XML documents must, by definition, be well formed. Do the following:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp69045"> </a><div class="pSmartList1"><li>Copy <code class="cCode">slideSample02.xml</code> to <code class="cCode">slideSampleBad1.xml</code>.</li></div><a name="wp69046"> </a><div class="pSmartList1"><li>Edit <code class="cCode">slideSampleBad1.xml</code> and remove the character shown below:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...<!-- OVERVIEW --><slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great</item> <item<span style="text-decoration: line-through">/</span>> <item>Who <em>buys</em> WonderWidgets</item></slide>...<a name="wp69047"> </a></pre></div></ol></div><a name="wp69048"> </a><p class="pBody">to produce:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...<item>Why <em>WonderWidgets</em> are great</item><item><item>Who <em>buys</em> WonderWidgets</item> ...<a name="wp69049"> </a>Now you have a file that you can use to generate an error in any parser, any time. (XML parsers are required to generate a fatal error for this file, because the lack of an end-tag for the <item> element means that the XML structure is no longer <span style="font-style: oblique">well-formed</span>.)<a name="wp69065"> </a></pre></div><a name="wp67669"> </a><h3 class="pHeading2">Substituting and Inserting Text</h3><a name="wp67670"> </a><p class="pBody">In this section, you'll learn about: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp67671"> </a><div class="pSmartList1"><li> Handling Special Characters (<code class="cCode">"<"</code>, <code class="cCode">"&"</code>, and so on)</li></div><a name="wp67672"> </a><div class="pSmartList1"><li> Handling Text with XML-style syntax</li></div></ul></div><a name="wp67674"> </a><h4 class="pHeading3">Handling Special Characters</h4><a name="wp67675"> </a><p class="pBody">In XML, an entity is an XML structure (or plain text) that has a name. Referencing the entity by name causes it to be inserted into the document in place of the entity reference. To create an entity reference, the entity name is surrounded by an ampersand and a semicolon, like this: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> &entityName;<a name="wp67676"> </a></pre></div><a name="wp67677"> </a><p class="pBody">Later, when you learn how to write a DTD, you'll see that you can define your own entities, so that <code class="cCode">&yourEntityName;</code> expands to all the text you defined for that entity. For now, though, we'll focus on the predefined entities and character references that don't require any special definitions. </p><a name="wp67680"> </a><h5 class="pHeading4">Predefined Entities</h5><a name="wp67681"> </a><p class="pBody">An entity reference like <code class="cCode">&amp;</code> contains a name (in this case, "amp") between the start and end delimiters. The text it refers to (&) is substituted for the name, like a macro in a programming language. <a href="IntroXML4.html#wp67688">Table 2-1</a> shows the predefined entities for special characters.</p><div align="left"><table border="1" summary="Predefined Entities" id="wp67688"> <caption><a name="wp67688"> </a><div class="pTableTitle">Table 2-1 Predefined Entities</div></caption> <tr align="center"> <th><a name="wp67692"> </a><div class="pCellHeading"> Character</div></th> <th><a name="wp67694"> </a><div class="pCellHeading">Reference</div></th></tr> <tr align="left"> <td><a name="wp67696"> </a><div class="pCellBody"> &</div></td> <td><a name="wp67698"> </a><div class="pCellBody">&amp;</div></td></tr> <tr align="left"> <td><a name="wp67700"> </a><div class="pCellBody"> <</div></td> <td><a name="wp67702"> </a><div class="pCellBody">&lt;</div></td></tr> <tr align="left"> <td><a name="wp67704"> </a><div class="pCellBody"> ></div></td> <td><a name="wp67706"> </a><div class="pCellBody">&gt;</div></td></tr> <tr align="left"> <td><a name="wp67708"> </a><div class="pCellBody"> "</div></td> <td><a name="wp67710"> </a><div class="pCellBody">&quot; </div></td></tr> <tr align="left"> <td><a name="wp67712"> </a><div class="pCellBody"> '</div></td> <td><a name="wp67714"> </a><div class="pCellBody">&apos;</div></td></tr></table></div><p class="pBody"></p><a name="wp67717"> </a><h5 class="pHeading4">Character References </h5><a name="wp67718"> </a><p class="pBody">A character reference like <code class="cCode">&#147;</code> contains a hash mark (<code class="cCode">#</code>) followed by a number. The number is the Unicode value for a single character, such as 65 for the letter "A", 147 for the left-curly quote, or 148 for the right-curly quote. In this case, the "name" of the entity is the hash mark followed by the digits that identify the character.</p><hr><a name="wp67720"> </a><p class="pNote">Note: <a href="http://www.unicode.org/charts/" target="_blank">XML expects values to be specified in decimal. However, the Unicode charts at http://www.unicode.org/charts/ specify values in hexadecimal! So you'll need to do a conversion to get the right value to insert into your XML data set.</a></p><hr><a name="wp67725"> </a><h4 class="pHeading3">Using an Entity Reference in an XML Document</h4><a name="wp67726"> </a><p class="pBody">Suppose you wanted to insert a line like this in your XML document:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -