«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
欢迎大家访问,希望大家多多交流!
    Email:hello105@ustc.edu
    QQ: 7779112
    

我的分类(专题)

首页(63)
Xml收藏(3)
Java收藏(17)
心情(11)
其他(32)


最新日志
DataStage 开发中遇到的几个问题
Tar的详细用法(转自Linux伊甸园)
UNIX常用命令-目录及文件操作命令(z
The 38 Subsystems of
DataStage安装
回来了!
rpm使用
学校好冷清阿
科大怪谈(1)
科大怪谈(2)

最新回复
回复:DataStage 开发中遇到的几
回复:DataStage 开发中遇到的几
回复:发放WALLOP邀请
回复:发放WALLOP邀请
回复:发放WALLOP邀请
回复:发放WALLOP邀请
回复:发放WALLOP邀请
回复:发放WALLOP邀请
回复:发放WALLOP邀请
回复:发放WALLOP邀请

留言板
签写新留言

为什么?
回学校了
关于wallop

统计
blog名称:hello105
日志总数:63
评论数量:174
留言数量:3
访问次数:396022
建立时间:2004年11月8日

链接

Lost Ferry

 




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) | 编辑 | 精华


发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.199 second(s), page refreshed 144766113 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号