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

📄 jaxpxslt8.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
&nbsp;&nbsp;&nbsp;&nbsp; // Set up the output stream&nbsp;&nbsp;&nbsp;&nbsp;StreamResult result = new StreamResult(System.out);&nbsp;&nbsp;// Set up the transformer to process the SAX events generated&nbsp;&nbsp;// by the last filter in the chain&nbsp;&nbsp;&nbsp;&nbsp;Transformer transformer = stf.newTransformer();&nbsp;&nbsp;&nbsp;&nbsp;SAXSource transformSource = new SAXSource(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filter2, input);&nbsp;&nbsp;&nbsp;&nbsp;transformer.transform(transformSource, result);&nbsp;&nbsp;} catch (...) {&nbsp;&nbsp;&nbsp;&nbsp;...<a name="wp84054"> </a></pre></div><a name="wp65412"> </a><p class="pBody">Notes:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp65413"> </a><div class="pSmartList1"><li>The Xalan transformation engine currently requires a namespace-aware SAX parser.</li></div><a name="wp84069"> </a><div class="pSmartList1"><li>This weird bit of code is explained by the fact that <code class="cCode">SAXTransformerFactory</code> extends <code class="cCode">TransformerFactory</code>, adding methods to obtain filter objects. The <code class="cCode">newInstance()</code> method is a static method defined in <code class="cCode">TransformerFactory</code>, which (naturally enough) returns a <code class="cCode">TransformerFactory</code> object. In reality, though, it returns a <code class="cCode">SAXTransformerFactory</code>. So, to get at the extra methods defined by <code class="cCode">SAXTransformerFactory</code>, the return value must be cast to the actual type.</li></div><a name="wp69647"> </a><div class="pSmartList1"><li>An <code class="cCode">XMLFilter</code> object is both a SAX reader and a SAX content handler. As a SAX reader, it generates SAX events to whatever object has registered to receive them. As a content handler, it consumes SAX events generated by its &quot;parent&quot; object -- which is, of necessity, a SAX reader, as well. (Calling the event generator a &quot;parent&quot; must make sense when looking at the internal architecture. From an external perspective, the name doesn't appear to be particularly fitting.) The fact that filters both generate and consume SAX events allows them to be chained together. </li></div></ol></div><a name="wp65416"> </a><h3 class="pHeading2">Understanding How the Filter Chain Works</h3><a name="wp65420"> </a><p class="pBody">The code listed above shows you how to set up the transformation. <a  href="JAXPXSLT8.html#wp65426">Figure 7-2</a> should help you understand what's happening when it executes. </p><a name="wp65424"> </a><p class="pBody"></p><div align="left"><img src="images/Fig8-22.gif" height="191" width="447" alt="Operation of Chained Filters: Source data set is parsed by parser, sending events to filter 1, which invoked it with parse() instruction. Filter 1 chains to filter 2, which chains to the transformer. Transformer creates the Result data set." border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="65426"> </a><strong><font >Figure 7-2    Operation of Chained Filters</font></strong></p><a name="wp65427"> </a><p class="pBody">When you create the transformer, you pass it at a <code class="cCode">SAXSource</code> object, which encapsulates a reader (in this case, <code class="cCode">filter2</code>) and an input stream. You also pass it a pointer to the result stream, where it directs its output. The diagram shows what happens when you invoke <code class="cCode">transform()</code> on the transformer. Here is an explanation of the steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp65428"> </a><div class="pSmartList1"><li>The transformer sets up an internal object as the content handler for <code class="cCode">filter2</code>, and tells it to parse the input source.</li></div><a name="wp65429"> </a><div class="pSmartList1"><li><code class="cCode">filter2</code>, in turn, sets itself up as the content handler for <code class="cCode">filter1</code>, and tells <span style="font-style: italic">it </span>to parse the input source.</li></div><a name="wp65430"> </a><div class="pSmartList1"><li><code class="cCode">filter1</code>, in turn, tells the <code class="cCode">parser</code> object to parse the input source.</li></div><a name="wp65431"> </a><div class="pSmartList1"><li>The <code class="cCode">parser</code> does so, generating SAX events which it passes to <code class="cCode">filter1</code>.</li></div><a name="wp65432"> </a><div class="pSmartList1"><li><code class="cCode">filter1</code>, acting in its capacity as a content handler, processes the events and does its transformations. Then, acting in its capacity as a SAX reader (<code class="cCode">XMLReader</code>), it sends SAX events to <code class="cCode">filter2</code>.</li></div><a name="wp65433"> </a><div class="pSmartList1"><li><code class="cCode">filter2</code> does the same, sending its events to the transformer's content handler, which generates the output stream.</li></div></ol></div><a name="wp65435"> </a><h3 class="pHeading2">Testing the Program</h3><a name="wp65436"> </a><p class="pBody">To try out the program, you'll create an XML file based on a tiny fraction of the XML DocBook format, and convert it to the <code class="cCode">ARTICLE</code> format defined here. Then you'll apply the <code class="cCode">ARTICLE</code> stylesheet to generate an HTML version.</p><hr><a name="wp65437"> </a><p class="pNote">Note: This example processes <code class="cCode"><a  href="../examples/jaxp/xslt/samples/small-docbook-article.xml" target="_blank">small-docbook-article.xml</a></code> using <code class="cCode"><a  href="../examples/jaxp/xslt/samples/docbookToArticle.xsl" target="_blank">docbookToArticle.xsl</a></code> and <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1c.xsl" target="_blank">article1c.xsl</a></code>. The result is <code class="cCode"><a  href="../examples/jaxp/xslt/samples/filterout.html" target="_blank">filterout.html</a></code> (The browser-displayable versions are <code class="cCode"><a  href="../examples/jaxp/xslt/samples/small-docbook-article-xml.html" target="_blank">small-docbook-article-xml.html</a></code>, <code class="cCode"><a  href="../examples/jaxp/xslt/samples/docbookToArticle-xsl.html" target="_blank">docbookToArticle-xsl.html</a></code>, <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1c-xsl.html" target="_blank">article1c-xsl.html</a></code>, and <code class="cCode"><a  href="../examples/jaxp/xslt/samples/filterout-src.html" target="_blank">filterout-src.html</a></code>.) See the O'Reilly Web pages for a good description of the DocBook article format.</p><hr><a name="wp65438"> </a><p class="pBody">Start by creating a small article that uses a minute subset of the XML DocBook format: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;?xml version=&quot;1.0&quot;?&gt;&lt;Article&gt;&nbsp;&nbsp;&lt;ArtHeader&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Title&gt;Title of my (Docbook) article&lt;/Title&gt;&nbsp;&nbsp;&lt;/ArtHeader&gt;&nbsp;&nbsp;&lt;Sect1&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Title&gt;Title of Section 1.&lt;/Title&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Para&gt;This is a paragraph.&lt;/Para&gt;&nbsp;&nbsp;&lt;/Sect1&gt;&lt;/Article&gt;<a name="wp65439"> </a></pre></div><a name="wp65440"> </a><p class="pBody">Next, create a stylesheet to convert it into the <code class="cCode">ARTICLE</code> format:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;xsl:stylesheet &nbsp;&nbsp;xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; &nbsp;&nbsp;version=&quot;1.0&quot;&nbsp;&nbsp;&gt;&nbsp;&nbsp;&lt;xsl:output method=&quot;xml&quot;/&gt; <code class="cCodeBold">(see Note #1)</code>&nbsp;&nbsp; &lt;xsl:template match=&quot;/&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ARTICLE&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:apply-templates/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ARTICLE&gt;&nbsp;&nbsp;&lt;/xsl:template&gt;&nbsp;&nbsp;&lt;!-- Lower level titles strip element tag --&gt; <code class="cCodeBold">(see Note #2)</code>&nbsp;&nbsp;&lt;!-- Top-level title --&gt;&nbsp;&nbsp;&lt;xsl:template match=&quot;/Article/ArtHeader/Title&quot;&gt; <code class="cCodeBold">(Note #3)</code>&nbsp;&nbsp;&nbsp;&nbsp;&lt;TITLE&gt; &lt;xsl:apply-templates/&gt; &lt;/TITLE&gt;&nbsp;&nbsp;&lt;/xsl:template&gt;&nbsp;&nbsp; &lt;xsl:template match=&quot;//Sect1&quot;&gt; <code class="cCodeBold">(see Note #4)</code>&nbsp;&nbsp;&nbsp;&nbsp;&lt;SECT&gt;&lt;xsl:apply-templates/&gt;&lt;/SECT&gt;&nbsp;&nbsp;&lt;/xsl:template&gt;&nbsp;&nbsp; &lt;xsl:template match=&quot;Para&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;PARA&gt;&lt;xsl:apply-templates/&gt;&lt;/PARA&gt; <code class="cCodeBold">(see Note #5)</code>&nbsp;&nbsp;&lt;/xsl:template&gt;&lt;/xsl:stylesheet&gt;<a name="wp65441"> </a></pre></div><a name="wp65442"> </a><p class="pBody">Notes:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp65443"> </a><div class="pSmartList1"><li>This time, the stylesheet is generating XML output.</li></div><a name="wp65444"> </a><div class="pSmartList1"><li>The template that follows (for the top-level title element) matches only the main title. For section titles, the <code class="cCode">TITLE</code> tag gets stripped. (Since no template conversion governs those title elements, they are ignored. The text nodes they contain, however, are still echoed as a result of XSLT's built in template rules-- so only the tag is ignored, not the text. More on that below.)</li></div><a name="wp65445"> </a><div class="pSmartList1"><li>The title from the DocBook article header becomes the <code class="cCode">ARTICLE</code> title.</li></div><a name="wp65446"> </a><div class="pSmartList1"><li>Numbered section tags are converted to plain <code class="cCode">SECT</code> tags.</li></div><a name="wp65447"> </a><div class="pSmartList1"><li>This template carries out a case conversion, so <code class="cCode">Para</code> becomes <code class="cCode">PARA</code>.</li></div></ol></div><a name="wp65448"> </a><p class="pBody">Although it hasn't been mentioned explicitly, XSLT defines a number of built-in (default) template rules. The complete set is listed in Section 5.8 of the specification. Mainly, they provide for the automatic copying of text and attribute nodes, and for skipping comments and processing instructions. They also dictate that inner elements are processed, even when their containing tags don't have templates. That is the reason that the text node in the section title is processed, even though the section title is not covered by any template.</p><a name="wp65449"> </a><p class="pBody">Now, run the <code class="cCode">FilterChain</code> program, passing it the stylesheet above (<code class="cCode"><a  href="../examples/jaxp/xslt/samples/docbookToArticle.xsl" target="_blank">docbookToArticle.xsl</a></code>), the <code class="cCode">ARTICLE</code> stylesheet (<code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1c.xsl" target="_blank">article1c.xsl</a></code>), and the small DocBook file (<code class="cCode"><a  href="../examples/jaxp/xslt/samples/small-docbook-article.xml" target="_blank">small-docbook-article.xml</a></code>), in that order. The result should like this: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;html&gt;&lt;body&gt;&lt;h1 align=&quot;center&quot;&gt;Title of my (Docbook) article&lt;/h1&gt;&lt;h2&gt;Title of Section 1.&lt;/h2&gt;&lt;p&gt;This is a paragraph.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;<a name="wp67378"> </a></pre></div><hr><a name="wp67379"> </a><p class="pNote">Note: This output was generated using JAXP 1.0. However, the first filter in the chain is not currently translating any of the tags in the input file. Until that defect is fixed, the output you see will consist of concatenated plain text in the HTML output, like this: &quot;<code class="cCode">Title of my (Docbook) article Title of Section 1. This is a paragraph.</code>&quot;.</p><hr><a name="wp67366"> </a><h3 class="pHeading2">Conclusion</h3><a name="wp65452"> </a><p class="pBody">Congratulations! You have completed the XSLT tutorial. There is a lot you can do with XML and XSLT, and you are now prepared to explore the many exciting possibilities that await.</p>    </blockquote>   <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JAXPXSLT7.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPXSLT9.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font>  </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -