« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
|
公告 |
欢迎大家访问,希望大家多多交流!
Email:hello105@ustc.edu
QQ: 7779112
|
统计 |
blog名称:hello105 日志总数:63 评论数量:174 留言数量:3 访问次数:396022 建立时间:2004年11月8日 |
| 
|
W3CHINA Blog首页 管理页面 写新日志 退出
[Xml收藏]学习Xalan转换XSLT 一 基本转换(zz) |
音乐昆虫 发表于 2004/11/8 17:24:01 |
学习Xalan转换XSLT 一 基本转换- -
XSLT的转换引擎有很多,这里是应用 apache 的项目 Xalan-java 2.6.0 来实现转换。
简单来说,分为四个步骤:
1,创建 内容源 和 样式源,象这样:
StreamSource contentSource = new StreamSource("http://localhost:8080/BISWeb/helloworld.xml");
StreamSource styleSource = new StreamSource("http://localhost:8080/BISWeb/helloworld.xsl");
2,确定结果输出的目的地:
StreamResult result = new StreamResult(out);
这是将结果输出到屏幕,你也可以输出到文件:
StreamResult result = new StreamResult(new FileOutputStream("WebRoot/output/helloworld.html"));
3,用 样式源 作为参数创建 transformer 对象:
Transformer transformer = transformerFactory.newTransformer(styleSource);
4,执行转换:
transformer.transform(contentSource, result);
下面与一个 servlet 转换输出到屏幕例子的源码:
helloworld.xml:
<?xml version="1.0"?> <doc>Hello World</doc>
helloworld.xsl:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="doc"> <out><xsl:value-of select="."/></out> </xsl:template> </xsl:stylesheet>
helloworld.java:
import java.io.IOException;import java.io.PrintWriter;
import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;
public class XSLServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); try { //step 1: Create content source and style source StreamSource contentSource = new StreamSource("http://localhost:8080/BISWeb/helloworld.xml"); StreamSource styleSource = new StreamSource("http://localhost:8080/BISWeb/helloworld.xsl"); //step 2: confirm tag of result StreamResult result = new StreamResult(out); //step 3: create Transformer object by using style source TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(styleSource); //step 4: excute transform transformer.transform(contentSource, result); } catch(TransformerConfigurationException e) { out.print("TransformerConfigurationException: "+e.getMessage()); } catch (TransformerException e) { out.print("TransformerException:"+e.getMessage()); } }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }
public void init() throws ServletException { // Put your code here }
|
阅读全文(2628) | 回复(0) | 编辑 | 精华 |
|