如何得到XML经XSL转换后的结果?

mystery_boy 2003-10-20 04:35:46
我有些xml数据,它有一组对应的很复杂的XSL文件,其中甚至还有大量的javascript代码,我想用程序取得这些xml数据经xsl文件转换后得到的数据,我用Transformer.transform处理时到用到了脚本的地方就出错了,请问一下该如何才能实现,谢谢各位
...全文
27 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mystery_boy 2003-10-22
  • 打赏
  • 举报
回复
非常感谢你的回答,我把你说的例子用我自己的程序也试过了,也能成功,但是用我自己的文件却不行,我不明白的是2.xsl为什么要做那样的改动。我的xsl文件开头是这样的:
<?xml version="1.0" encoding="EUC-JP" ?>
<!DOCTYPE stylesheet>

<xsl:stylesheet version="1.0"
exclude-result-prefixes="msxsl jp local xql"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:jp="http://www.jpo.go.jp"
xmlns:local="#local-functions"
xmlns:xql="#xql-functions">

<xsl:include href="gat-com.xsl" />
<xsl:include href="gat-JS-com.xsl" />
<xsl:include href="application-body.xsl" />
<xsl:include href="gat-a-com.xsl" />

<xsl:template match="/">
<html>
<head>

我用程序处理时,只能出来一部分结果,然后报错,我的工作环境是中文win2000切换到日文字符下面,做出来的东西也是要给日本人用的,不至是不是字符环境的问题,trans.setOutputProperty(OutputKeys.ENCODING," ");里面不知改些什么,反正xsl于xml文件都是EUC-JP的,我的程序很简单如下:
public class Transform {

/**
* Performes an XSLT transformation, sending the results
* to System.out.
*/
public static void main(String[] args) throws Exception {

File xmlFile = new File(xml);
File xsltFile = new File(xsl);
String outputHtml = "output.html";
Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
Result dest = new StreamResult(new File(outputHtml));

try{
trans.transform(xmlSource,dest
} catch(Exception e) {
System.out.println(e);
}
}
}
liad 2003-10-21
  • 打赏
  • 举报
回复
代码片断
<%@ page contentType="text/html; charset=gb2312" errorPage="systemerror.jsp" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="javax.xml.parsers.*"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.xml.transform.*"%>
<%@ page import="javax.xml.transform.stream.*"%>
<%
StringReader reader=new StringReader(你的xml);
StreamSource xml = new StreamSource(reader);
StreamSource xsl = new StreamSource(xsl文件路径);
StreamResult result = new StreamResult(out);

Transformer trans = TransformerFactory.newInstance().newTransformer(xsl);

//trans.setOutputProperty(OutputKeys.ENCODING,"GB2312");
trans.setOutputProperty(OutputKeys.METHOD,"html");
trans.setOutputProperty(OutputKeys.VERSION,"4.0");
//trans.setOutputProperty("encoding","UTF-8");
trans.transform(xml, result);

%>
mystery_boy 2003-10-21
  • 打赏
  • 举报
回复
好像这里搞java的都不喜欢说话啊.......
liad 2003-10-21
  • 打赏
  • 举报
回复
下面用CSDN的2375514.xml(本页)和2.xsl来测试

一、
2.xsl前面的几行改为:
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>

二、注意要使用如下语句
// Create a builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

三、转换的源文件为
// Transform.java
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Transform
{
public static void main(String[] args)
{
String inputXml = "2375514.xml";
String outputHtml = "2375514.html";
String transXsl = "2.xsl";

Document xmlDoc = parseXmlFile(inputXml, false);
Document xslDoc = parseXmlFile(transXsl, false);

// Save the document to the disk file
try
{
TransformerFactory tranFactory = TransformerFactory.newInstance();
DOMSource xslSource = new DOMSource(xslDoc);
Transformer aTransformer = tranFactory.newTransformer(xslSource);
// aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); // OutputKeys.ENCODING == "encoding"
aTransformer.setOutputProperty("encoding","GB2312");
Source src = new DOMSource(xmlDoc);
Result dest = new StreamResult(new File(outputHtml));
aTransformer.transform(src, dest);
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
}
catch (TransformerException e)
{
e.printStackTrace();
}
}

// Parses an XML file and returns a DOM document.
// If validating is true, the contents is validated against the DTD
// specified in the file.
public static Document parseXmlFile(String filename, boolean validating)
{
try {
// Create a builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validating);
factory.setNamespaceAware(true);

// Create the builder and parse the file
Document doc = factory.newDocumentBuilder().parse(new File(filename));
return doc;
} catch (SAXException e) {
e.printStackTrace();
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

四、
输出文件为2375514.html,没有抛出异常;如果不修改2.xsl,将抛出异常

五、你可以缩减你自己的代码,增量测试

mystery_boy 2003-10-21
  • 打赏
  • 举报
回复
没错啊,我也是这么些的,不过在显示了一部分结果后就提示下面的错误:
javax.xml.transform.TransformerException: java.lang.NoSuchMethodException
: For extension function, could not find method java.lang.String.eval_conv_for_p
ublication_number_tokkyo_jituyou_bango([ExpressionContext,] #STRING, #NODESET).

我看了一下eval_conv_for_publication_number_tokkyo_jituyou_bango其实是XSL脚本中的一个方法名,不知是什么问题?

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧