📄 jaxpxslt6.html
字号:
</p><hr><a name="wp65031"> </a><p class="pBody">Start by copying <code class="cCode">TransformationApp02</code>, which parses an XML file and writes to <code class="cCode">System.out</code>. Save it as <code class="cCode">Stylizer.java</code>.</p><a name="wp65032"> </a><p class="pBody">Next, modify occurrences of the class name and the usage section of the program:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class <code class="cCodeStruck">TransformationApp</code><code class="cCodeBold">Stylizer</code> { if (argv.length != <code class="cCodeStruck">1</code><code class="cCodeBold"> 2</code>) { System.err.println ( <code class="cCodeStruck">"Usage: java TransformationApp filename");</code> "Usage: java <code class="cCodeBold">Stylizer stylesheet xmlfile</code>"); System.exit (1); } ...<a name="wp65033"> </a></pre></div><a name="wp65034"> </a><p class="pBody">Then modify the program to use the stylesheet when creating the <code class="cCode">Transformer</code> object. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...import javax.xml.transform.dom.DOMSource; <code class="cCodeBold">import javax.xml.transform.stream.StreamSource; </code>import javax.xml.transform.stream.StreamResult; ...<a name="wp65035"> </a>public class Stylizer { ... public static void main (String argv[]) { ... try { <code class="cCodeStruck">File f = new File(argv[0]);</code> <code class="cCodeBold">File stylesheet = new File(argv[0]); File datafile = new File(argv[1]);</code> DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(<code class="cCodeStruck">f</code> <code class="cCodeBold">datafile</code>); ... <code class="cCodeBold"> StreamSource stylesource = new StreamSource(stylesheet); </code> Transformer transformer = Factory.newTransformer(<code class="cCodeBold">stylesource</code>); ...<a name="wp65036"> </a></pre></div><a name="wp65037"> </a><p class="pBody">This code uses the file to create a <code class="cCode">StreamSource</code> object, and then passes the source object to the factory class to get the transformer. </p><hr><a name="wp65038"> </a><p class="pNote">Note: You can simplify the code somewhat by eliminating the <code class="cCode">DOMSource</code> class entirely. Instead of creating a <code class="cCode">DOMSource</code> object for the XML file, create a <code class="cCode">StreamSource</code> object for it, as well as for the stylesheet.</p><hr><a name="wp65039"> </a><p class="pBody">Now compile and run the program using <code class="cCode">article1a.xsl</code> on <code class="cCode">article1.xml</code>. The results should look like this: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><html><body><h1 align="center">A Sample Article</h1><h2>The First Major Section </h2><p>This section will introduce a subsection.</p><h3>The Subsection Heading </h3><p>This is the text of the subsection. </p></body></html><a name="wp85705"> </a></pre></div><a name="wp65048"> </a><p class="pBody">At this point, there is quite a bit of excess whitespace in the output. You'll see how to eliminate most of it in the next section. </p><a name="wp65050"> </a><h3 class="pHeading2">Trimming the Whitespace</h3><a name="wp65051"> </a><p class="pBody">If you recall, when you took a look at the structure of a DOM, there were many text nodes that contained nothing but ignorable whitespace. Most of the excess whitespace in the output came from these nodes. Fortunately, XSL gives you a way to eliminate them. (For more about the node structure, see <a href="JAXPXSLT3.html#wp69428">The XSLT/XPath Data Model</a>.)</p><hr><a name="wp65055"> </a><p class="pNote">Note: The stylesheet described here is <code class="cCode"><a href="../examples/jaxp/xslt/samples/article1b.xsl" target="_blank">article1b.xsl</a></code>. The result is <code class="cCode"><a href="../examples/jaxp/xslt/samples/stylizer1b.html" target="_blank">stylizer1b.html</a></code>. (The browser-displayable versions are <code class="cCode"><a href="../examples/jaxp/xslt/samples/article1b-xsl.html" target="_blank">article1b-xsl.html</a></code> and <code class="cCode"><a href="../examples/jaxp/xslt/samples/stylizer1b-src.html" target="_blank">stylizer1b-src.html</a></code>.)</p><hr><a name="wp65056"> </a><p class="pBody">To remove some of the excess whitespace, add the line highlighted below to the stylesheet.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><xsl:stylesheet ... > <xsl:output method="html"/> <code class="cCodeBold"> <xsl:strip-space elements="SECT"/></code> ...<a name="wp65057"> </a></pre></div><a name="wp65058"> </a><p class="pBody">This instruction tells XSL to remove any text nodes under <code class="cCode">SECT</code> elements that contain nothing but whitespace. Nodes that contain text other than whitespace will not be affected, and other kinds of nodes are not affected. </p><a name="wp65059"> </a><p class="pBody">Now, when you run the program, the result looks like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><html><body><h1 align="center">A Sample Article</h1><h2>The First Major Section </h2><p>This section will introduce a subsection.</p><h3>The Subsection Heading </h3><p>This is the text of the subsection. </p></body></html><a name="wp65060"> </a></pre></div><a name="wp65064"> </a><p class="pBody">That's quite an improvement. There are still newline characters and white space after the headings, but those come from the way the XML is written:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><SECT>The First Major Section<code class="cCodeBold">____</code><PARA>This section will introduce a subsection.</PARA>^^^^<a name="wp65065"> </a></pre></div><a name="wp65066"> </a><p class="pBody">Here, you can see that the section heading ends with a newline and indentation space, before the <code class="cCode">PARA</code> entry starts. That's not a big worry, because the browsers that will process the HTML routinely compress and ignore the excess space. But there is still one more formatting tool at our disposal.</p><hr><a name="wp65067"> </a><p class="pNote">Note: The stylesheet described here is <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/stylizer1c.html" target="_blank">stylizer1c.html</a></code>. (The browser-displayable versions are <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/stylizer1c-src.html" target="_blank">stylizer1c-src.html</a></code>.)</p><hr><a name="wp65068"> </a><p class="pBody">To get rid of that last little bit of whitespace, add this template to the stylesheet: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold"> <xsl:template match="text()"> <xsl:value-of select="normalize-space()"/> </xsl:template></code></xsl:stylesheet><a name="wp65069"> </a></pre></div><a name="wp65070"> </a><p class="pBody">The output now looks like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><html><body><h1 align="center">A Sample Article</h1><h2>The First Major Section</h2><p>This section will introduce a subsection.</p><h3>The Subsection Heading</h3><p>This is the text of the subsection.</p></body></html><a name="wp65071"> </a></pre></div><a name="wp65072"> </a><p class="pBody">That is quite a bit better. Of course, it would be nicer if it were indented, but that turns out to be somewhat harder than expected! Here are some possible avenues of attack, along with the difficulties:</p><a name="wp65073"> </a><p class="pDefinitionTerm">Indent option</p><a name="wp65074"> </a><p class="pDefinition">Unfortunately, the <code class="cCode">indent="yes"</code> option that can be applied to XML output is not available for HTML output. Even if that option were available, it wouldn't help, because HTML elements are rarely nested! Although HTML source is frequently indented to show the <span style="font-style: italic">implied</span> structure, the HTML tags themselves are not nested in a way that creates a <span style="font-style: italic">real</span> structure.</p><a name="wp65075"> </a><p class="pDefinitionTerm">Indent variables</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -