Dùng JSP transform tài liệu XML thành HTML
Tạo file products.xml có nội dung sau
|
<?xml version=“1.0″ encoding=“utf-8″?> <products> <item> <code>A01</code> <name>CocaCola</name> <price>$2</price> <description>An aerated drink</description> </item> <item> <code>A02</code> <name>Xoai</name> <price>$5</price> <description>A delicous juice</description> </item> <item> <code>A03</code> <name>Kraft cheese</name> <price>$10</price> <description>Best Cheese</description> </item> </products> |
Tạo file product_format.xsl có nội dung sau
|
<?xml version=“1.0″ encoding=“utf-8″?> <xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” version=“1.0″> <xsl:output method=“html” indent=“yes” /> <xsl:template match=“/”> <html> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match=“products”> <table border=“2″ width=“30%”> <xsl:for-each select=“item”> <tr> <td> <i> <xsl:value-of select=“code” /> </i> </td> <td> <xsl:value-of select=“name” /> </td> <td> <xsl:value-of select=“price” /> </td> <td> <xsl:value-of select=“description” /> </td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> |
Tạo file product.jsp có nội dung sau
|
<?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”> <%@ page language=“java” contentType=“text/html; charset=ISO-8859-1″ pageEncoding=“ISO-8859-1″%> <%@taglib prefix=“c” uri=“http://java.sun.com/jstl/core”%> <%@taglib prefix=“x” uri=“http://java.sun.com/jstl/xml”%> <html> <head> <title>JSTL importing XML – XSL Document</title> </head> <body> <h3>Products Informations</h3> <c:import url=“http://localhost:8080/XML_JSP/ex1/products.xml” var=“inputDoc” />
<c:import url=“http://localhost:8080/XML_JSP/ex1/product_format.xsl” var=“stylesheet” /> <x:transform xml = “${inputDoc}“ xslt = “${stylesheet}“/> </body> </html>
|
Kết quả kết xuất trên trình duyệt

darkrider said
Cái uri=“http://java.sun.com/jstl/core” hình như đã cũ rồi bạn ơi, trên Java Sun Documentation nó dùng
uri=“http://java.sun.com/jsp/jstl/core”
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
Nhưng ko hiểu sao mình thử trên free host JSP thì nó cứ báo cannot resolved.
Võ Văn Hải said
Bạn dùng http://java.sun.com/jsp/jstl/core thế nào cũng lỗi, cũ mới là thế nào?
Nguyễn Kiên said
ý nghĩa 2 câu lệnh này là gì. sao nó cứ bị lỗi hoài, chạy không được bạn ơi ?
Nguyễn Kiên said
ý nghĩa là gì vậy sao lỗi hoài
Tiến Đạt said
Vậy cứ mỗi lần muốn chuyển bất kì 1 file xml nào là lại phải viết 1 file xsl hả thầy?
Có cách nào mà viết 1 file xsl chung cho tất cả các loại file xml để chuyển nó dễ dàng hơn không thầy?
Võ Văn Hải said
Nếu em đủ khả năng viết 1 file xsl cho mọi tình huống dữ liệu thì cứ việc. Còn tôi thì tôi không thể viết được 1 file như thế.
.
Quốc Viêt said
thầy ơi cho em hỏi
e đã add thư viên jstl 1.2.jar vao library rồi,lúc khởi tạo 1 trang jsp mới,khi khai báo như thế này
thì nó báo lỗi cannot be reosobved là sao vậy thầy! cám ơn thầy!
Võ Văn Hải said
em nói rõ hơn thử xem. thầy không hiểu ý em.
Quoc Viet said
dạ vâng,e cám ơn thầy! lỗi này lúc e reset lại thì hết! e cám ơn thầy!
Quoc Viet said
àh! thầy cho em hỏi! em đang làm 1 cái project về web service! em dùng thư viện jstl để load dữ liệu lấy từ csdl lên!(giống trang hiển thị như bài demo của thầy làm ở trên),lúc e dùng chức năng tìm kiếm 1 tên,lúc chạy,mặc dù nó ko báo lỗi,nhưng lúc hiển thị ra thì chỉ ra các ô trống,chứ ko load đc dữ liệu,em đã chạy debug,thì xem kết quả trả về là null! nên nó ko ra đc! thầy cho em hỏi là vì sao nó bị vậy ạ! có fải là do jstl hay ko?