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

📄 lib0077.html

📁 java外企软件工程师就业班 J2EE方向 《J2EE架构师手册》 电子书
💻 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">&lt;purchase-order&gt;</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">
&lt;xsl:stylesheet version="1.0"
  xmlns:xsl=" http://www.w3.org/1999/XSL/Transform"&gt;
&lt;xsl:output method=" html"/&gt;
&lt;xsl:template match=" customer-order"&gt;
  &lt;p&gt;
    Order Nbr: &lt;xsl:value-of select="@order-id" /&gt;
    Date Created: &lt;xsl:value-of select="@date-created" /&gt;
    Date Shipped: &lt;xsl:value-of select="@date-shipped" /&gt;
  &lt;/p&gt;
  &lt;xsl:for-each select = "order-line"&gt;
    &lt;li&gt;
      Product: &lt;xsl:value-of select="@product-id" /&gt;
      Quantity: &lt;xsl:value-of select="@order-quantity" /&gt;
      Price: &lt;xsl:value-of select="@order-price" /&gt;
  &lt;/li&gt;
&lt;/xsl:for-each&gt;<a name="367"></a><a name="IDX-151"></a>
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</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 + -