?? lib0077.html
字號:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Using XSLT Within Java</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0076.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0078.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch11"></a>
<div class="section">
<h2 class="first-section-title"><a name="365"></a><a name="ch11lev1sec5"></a>Using XSLT Within Java</h2><p class="first-para">XSLT and X/Path are often used in combination to reformat XML documents into a readable format, such as HTML or text. In essence, these technologies are used mostly to provide "reporting" capabilities for XML documents. Additionally, XSLT and X/Path can transform an XML document into any text format (e.g., another XML document or source code). This section assumes that you have a basic knowledge of XML, X/Path, and XSLT and focuses on how you can use them within the Java language. If you need a better understanding of these technologies, consult the tutorials at Zvon (<a target="_top" class="url" href="http://www.zvon.org/">http://www.zvon.org/</a>).</p>
<p class="para">XSLT is the mechanism by which XML data is transformed into HTML or text format. The details about this transformation are stored in an XSL style sheet. <a class="internaljump" href="#ch11list08">Listing 11.8</a> is a sample style sheet for the <span class="fixed"><purchase-order></span> example we've been using. This style sheet produces an HTML page listing all customer orders and ordered items.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.8: </span>Sample XSL Style Sheet</span><a name="366"></a><a name="ch11list08"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
<xsl:stylesheet version="1.0"
xmlns:xsl=" http://www.w3.org/1999/XSL/Transform">
<xsl:output method=" html"/>
<xsl:template match=" customer-order">
<p>
Order Nbr: <xsl:value-of select="@order-id" />
Date Created: <xsl:value-of select="@date-created" />
Date Shipped: <xsl:value-of select="@date-shipped" />
</p>
<xsl:for-each select = "order-line">
<li>
Product: <xsl:value-of select="@product-id" />
Quantity: <xsl:value-of select="@order-quantity" />
Price: <xsl:value-of select="@order-price" />
</li>
</xsl:for-each><a name="367"></a><a name="IDX-151"></a>
</xsl:template>
</xsl:stylesheet>
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /xml/PurchaseOrder.xsl</p>
<p class="para">In a layered architecture, you would want to perform this transformation in a DAO. The HTML output from this transformation would be returned to the presentation tier for display to a user. <a class="internaljump" href="#ch11list09">Listing 11.9</a> is an example of how you could do this within Java.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.9: </span>Initiating an XSL Transformation</span><a name="368"></a><a name="ch11list09"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:import javax.xml.transform.Transformer;
2:import javax.xml.transform.TransformerException;
3:import javax.xml.transform.TransformerFactory;
4:import javax.xml.transform.stream.StreamResult;
5:import javax.xml.transform.stream.StreamSource;
6:
7:public class SampleXSL
8:{
9: public String runSimpleTransformation()
10: throws TransformerConfigurationException,
11: TransformerException
12: {
13: ByteArrayOutputStream output =
14: new ByteArrayOutputStream (200000);
15: TransformerFactory tFactory =
16: TransformerFactory.newInstance();
17: Transformer transformer = tFactory.newTransformer
18: (new StreamSource("PurchaseOrder.xsl"));
19: transformer.transform(
20: new StreamSource("PurchaseOrder.xml"),
21: new StreamResult(output) );
22: return output.toString();
23: }
24:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="last-para">
<i class="emphasis">Source:</i> /src/book/sample/dao/xml/SampleXSL.java</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0076.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0078.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -