Jsp結合XML+XSLT將輸出轉換為Html格式2

來源:互聯網
上載者:User
我們的做法是:

<%@taglib uri="xsl.jar" prefix="xsl" %>

  我們以Jakarta的XSL taglib附帶的Apply.jsp為例,正好瞭解一下Jsp XML XSLT三者之間的關係:

  Apply.jsp

<%@taglib uri="xsl.jar" prefix="xsl" %>
<html>
<head>
<title>Employee List</title>
</head>
<body bgcolor="white">

<p>下面展示了Jsp的四種組合XML XSLT的方法:
<p>下面使用apply方法,將已經存在的employees.xml和employeeList.xsl結合在一起

<xsl:apply xml="/xml/employees.xml" xsl="/xml/employeeList.xsl"/>
<hr>

<p>下面是使用已經存在employeeList.xsl 然後在Jsp中自己直接寫入XML資料.

<xsl:apply xsl="/xml/employeeList.xsl">
<?xml version="1.0" encoding="ISO-8859-1"?>
<employees>
<employee id="123">
<first-name>John</first-name>
<last-name>Doe</last-name>
<telephone>800-555-1212</telephone>
</employee>
<employee id="456">
<first-name>Jane</first-name>
<last-name>Smith</last-name>
<telephone>888-555-1212</telephone>
</employee>
<employee id="789">
<first-name>George</first-name>
<last-name>Taylor</last-name>
<telephone>555-555-1212</telephone>
</employee>
</employees>
</xsl:apply>
<hr>

<p>下面使使用include調用的辦法,這樣一個XSLT樣式可以適應不同的XML檔案。

<xsl:apply xsl="/xml/employeeList.xsl">
<xsl:include page="/xml/employees.xml"/>
</xsl:apply>
<hr>

<p>下面是使用import方法,在page-scope(類似scope="page")中匯入XML檔案</p>

<xsl:import id="data" page="/xml/employees.xml"/>
<xsl:apply nameXml="data" xsl="/xml/employeeList.xsl"/>

</body>

  在上面程式中,展示了四種Jsp組合XML XSLT的方法,基本可以滿足我們的需要。注意上面的XML檔案路徑是"/xml/",這是相對Tomcat容器的絕對路徑。

  我們簡單看一下employeeList.xsl和employees.xml內容:

  employeeList.xsl類似html中的CSS,主要是對XML中資料顯示方式進行定義:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="employees">
<table border="1" width="100%">
<tr>
<th>ID</th>
<th>Employee Name</th>
<th>Phone Number</th>
</tr>
<xsl:for-each select="employee">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<xsl:value-of select="last-name"/>,
<xsl:value-of select="first-name"/>
</td>
<td>
<xsl:value-of select="telephone"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

employees.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<employees>
 <employee id="123">
  <first-name>John</first-name>
  <last-name>Doe</last-name>
  <telephone>800-555-1212</telephone>
 </employee>

 <employee id="456">
  <first-name>Jane</first-name>
  <last-name>Smith</last-name>
  <telephone>888-555-1212</telephone>
 </employee>

  <employee id="789">
  <first-name>George</first-name>
  <last-name>Taylor</last-name>
  <telephone>555-555-1212</telephone>
 </employee>
</employees>

  如果我們在employees.xml頂部加入:

<?xml:stylesheet type="text/xsl" href="catalog.xsl"?>

  用支援XML的IE 5.0以上瀏覽器調用,其顯示頁面就和Apply.jsp顯示頁面是一樣的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.