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

📄 jaxpxslt8.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>Concatenating Transformations with a Filter Chain</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="JAXPXSLT7.html" />    <link rel="Next" href="JAXPXSLT9.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <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">    <blockquote><a name="wp72462"> </a><h2 class="pHeading1">Concatenating Transformations with a Filter Chain</h2><a name="wp65401"> </a><p class="pBody">It is sometimes useful to create a <span style="font-style: italic">filter chain</span> -- a concatenation of XSLT transformations in which the output of one transformation becomes the input of the next. This section of the tutorial shows you how to do that. </p><a name="wp65403"> </a><h3 class="pHeading2">Writing the Program</h3><a name="wp65404"> </a><p class="pBody">Start by writing a program to do the filtering. This example will show the full source code, but you can use one of the programs you've been working on as a basis, to make things easier.</p><hr><a name="wp65405"> </a><p class="pNote">Note: The code described here is contained in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/FilterChain.java" target="_blank">FilterChain.java</a></code>.</p><hr><a name="wp65406"> </a><p class="pBody">The sample program includes the import statements that identify the package locations for each class:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.InputSource;import org.xml.sax.XMLReader;import org.xml.sax.XMLFilter;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.stream.StreamSource;import javax.xml.transform.stream.StreamResult;import java.io.*;<a name="wp65407"> </a></pre></div><a name="wp65408"> </a><p class="pBody">The program also includes the standard error handlers you're used to. They're listed here, just so they are all gathered together in one place:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">}catch (TransformerConfigurationException tce) {&nbsp;&nbsp;// Error generated by the parser&nbsp;&nbsp;System.out.println (&quot;* Transformer Factory error&quot;);&nbsp;&nbsp;System.out.println(&quot;   &quot; + tce.getMessage() );&nbsp;&nbsp;// Use the contained exception, if any&nbsp;&nbsp;Throwable x = tce;&nbsp;&nbsp;if (tce.getException() != null)&nbsp;&nbsp;&nbsp;&nbsp;x = tce.getException();&nbsp;&nbsp;x.printStackTrace();}catch (TransformerException te) {&nbsp;&nbsp;// Error generated by the parser&nbsp;&nbsp;System.out.println (&quot;* Transformation error&quot;);&nbsp;&nbsp;System.out.println(&quot;   &quot; + te.getMessage() );&nbsp;&nbsp;// Use the contained exception, if any&nbsp;&nbsp;Throwable x = te;&nbsp;&nbsp;if (te.getException() != null)&nbsp;&nbsp;&nbsp;&nbsp;x = te.getException();&nbsp;&nbsp;x.printStackTrace();}catch (SAXException sxe) {&nbsp;&nbsp;// Error generated by this application&nbsp;&nbsp;// (or a parser-initialization error)&nbsp;&nbsp;Exception  x = sxe;&nbsp;&nbsp;if (sxe.getException() != null)&nbsp;&nbsp;&nbsp;&nbsp;x = sxe.getException();&nbsp;&nbsp;x.printStackTrace();}catch (ParserConfigurationException pce) {&nbsp;&nbsp;// Parser with specified options can&#39;t be built&nbsp;&nbsp;pce.printStackTrace();}catch (IOException ioe) {&nbsp;&nbsp;// I/O error&nbsp;&nbsp;ioe.printStackTrace();}<a name="wp65409"> </a></pre></div><a name="wp65410"> </a><p class="pBody">In between the import statements and the error handling, the core of the program consists of the code shown below.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public static void main (String argv[]){&nbsp;&nbsp;if (argv.length != 3) {&nbsp;&nbsp;&nbsp;&nbsp;System.err.println (&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Usage: java FilterChain style1 style2 xmlfile&quot;);&nbsp;&nbsp;&nbsp;&nbsp;System.exit (1);&nbsp;&nbsp;}&nbsp;&nbsp; try {&nbsp;&nbsp;&nbsp;&nbsp;// Read the arguments&nbsp;&nbsp;&nbsp;&nbsp;File stylesheet1 = new File(argv[0]);&nbsp;&nbsp;&nbsp;&nbsp;File stylesheet2 = new File(argv[1]);&nbsp;&nbsp;&nbsp;&nbsp;File datafile = new File(argv[2]);&nbsp;&nbsp;&nbsp;&nbsp; // Set up the input stream&nbsp;&nbsp;&nbsp;&nbsp;BufferedInputStream bis = new&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedInputStream(newFileInputStream(datafile));&nbsp;&nbsp;&nbsp;&nbsp;InputSource input = new InputSource(bis);&nbsp;&nbsp;&nbsp;&nbsp; // Set up to read the input file <code class="cCodeBold">(see Note #1)</code>&nbsp;&nbsp;&nbsp;&nbsp;SAXParserFactory spf = SAXParserFactory.newInstance();&nbsp;&nbsp;&nbsp;&nbsp;spf.setNamespaceAware(true);&nbsp;&nbsp;&nbsp;&nbsp;SAXParser parser = spf.newSAXParser();&nbsp;&nbsp;&nbsp;&nbsp;XMLReader reader = parser.getXMLReader();&nbsp;&nbsp;&nbsp;&nbsp; // Create the filters <code class="cCodeBold">(see Note #2)</code>&nbsp;&nbsp;&nbsp;&nbsp;SAXTransformerFactory stf =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(SAXTransformerFactory)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TransformerFactory.newInstance();&nbsp;&nbsp;&nbsp;&nbsp;XMLFilter filter1 = stf.newXMLFilter(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new StreamSource(stylesheet1));&nbsp;&nbsp;&nbsp;&nbsp;XMLFilter filter2 = stf.newXMLFilter(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new StreamSource(stylesheet2));&nbsp;&nbsp;&nbsp;&nbsp;// Wire the output of the reader to filter1 <code class="cCodeBold">(see Note #3)</code>&nbsp;&nbsp;&nbsp;&nbsp;// and the output of filter1 to filter2&nbsp;&nbsp;&nbsp;&nbsp;filter1.setParent(reader);&nbsp;&nbsp;&nbsp;&nbsp;filter2.setParent(filter1);

⌨️ 快捷键说明

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