看了第二遍,明白你意思了。 你所说的org.apache.xalan.xsltc.compiler.XSLTC 有很多种形式,你随便挑种就可以了,谁说只能从物理文件读取Document的?apache不会作出那么傻得类的。 boolean compile(InputSource input, java.lang.String name) Compiles an XSL stylesheet passed in through an InputStream boolean compile(java.io.InputStream stream, java.lang.String name) Compiles an XSL stylesheet passed in through an InputStream byte[][] compile(java.lang.String name, InputSource input) Compiles a stylesheet pointed to by a URL. byte[][] compile(java.lang.String name, InputSource input, int outputType) Compiles a stylesheet pointed to by a URL. boolean compile(java.net.URL url) Compiles an XSL stylesheet pointed to by a URL boolean compile(java.net.URL url, java.lang.String name) Compiles an XSL stylesheet pointed to by a URL boolean compile(java.util.Vector stylesheets) Compiles a set of stylesheets pointed to by a Vector of URLs ========================== 就拿这条把 compile public boolean compile(java.io.InputStream stream, java.lang.String name)Compiles an XSL stylesheet passed in through an InputStream Parameters: input - An InputStream that will pass in the stylesheet contents name - The name of the translet class to generate Returns: 'true' if the compilation was successful 我想InputStream不必是一定是物理文件吧。 上面的文件再改一下!public static void transformAndCompile(String stylesheet,String url,String className) throws JDOMException { try { SAXBuilder builder = new SAXBuilder(); Document in = builder.build(url); Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(stylesheet)); JDOMResult out = new JDOMResult(); transformer.transform(new JDOMSource(in), out); Document trasformed = out.getDeocument(); XMLOutputter xmlOutputter = new XMLOutputter(); // Output to the I/O stream InputStream resultIn = new InputStream(); OutputStream resultOut = new OutputStream(resultIn); xmlOutputter.output(trasformed , sourceOut); org.apache.xalan.xsltc.compiler.XSLTC xsltc = new org.apache.xalan.xsltc.compiler.XSLTC (); xsltc.compile(resultIn,className); } catch (TransformerException e) { throw new JDOMException("XSLT Trandformation failed", e); } catch(XXXExeption...){..} } 大致是这样吧,你再试试。
那怎么transform?
1、将一个XSL文件编译为一个class文件,例如xsl编译后className为com.isee.sample.Test;
2、进行XSLT时,通常代码如下:
Class tc = Class.forName(className);
AbstractTranslet translet = (AbstractTranslet)tc.newInstance();
...
DOMImpl dom = new DOMImpl(uri);// uri为XML的来源,可为filename,也可为URL
translet.transform(dom, ...);关键问题就出在这,dom必须从物理存储介质读取,要满足它的要求,内存中的XML字符串就必须进行一次IO。
DOMBuilder builder = new DOMBuilder();
Document doc = builder.build(url);
使用如下函数对你的数据源转换:
public static List transform(Document in, String stylesheet)
throws JDOMException {
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
return out.getResult();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
}
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(url);
============以上类除Transformer均在Jdom中定义,不是org.w3c.dom中的。
public static Document transform(Document in, String stylesheet,String url)
throws JDOMException {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(url);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
return out.getDeocument();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
}
public static Document transform(String stylesheet,String url)
throws JDOMException {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(url);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
return out.getDeocument();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
}这样就可以了。
public static Document transform(String stylesheet,String url)
throws JDOMException {
try {
SAXBuilder builder = new SAXBuilder();
Document in = builder.build(url);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
return out.getDeocument();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
}
谢谢了
你所说的org.apache.xalan.xsltc.compiler.XSLTC
有很多种形式,你随便挑种就可以了,谁说只能从物理文件读取Document的?apache不会作出那么傻得类的。
boolean compile(InputSource input, java.lang.String name)
Compiles an XSL stylesheet passed in through an InputStream
boolean compile(java.io.InputStream stream, java.lang.String name)
Compiles an XSL stylesheet passed in through an InputStream
byte[][] compile(java.lang.String name, InputSource input)
Compiles a stylesheet pointed to by a URL.
byte[][] compile(java.lang.String name, InputSource input, int outputType)
Compiles a stylesheet pointed to by a URL.
boolean compile(java.net.URL url)
Compiles an XSL stylesheet pointed to by a URL
boolean compile(java.net.URL url, java.lang.String name)
Compiles an XSL stylesheet pointed to by a URL
boolean compile(java.util.Vector stylesheets)
Compiles a set of stylesheets pointed to by a Vector of URLs
==========================
就拿这条把
compile
public boolean compile(java.io.InputStream stream,
java.lang.String name)Compiles an XSL stylesheet passed in through an InputStream
Parameters:
input - An InputStream that will pass in the stylesheet contents
name - The name of the translet class to generate
Returns:
'true' if the compilation was successful
我想InputStream不必是一定是物理文件吧。
上面的文件再改一下!public static void transformAndCompile(String stylesheet,String url,String className)
throws JDOMException {
try {
SAXBuilder builder = new SAXBuilder();
Document in = builder.build(url);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMResult out = new JDOMResult();
transformer.transform(new JDOMSource(in), out);
Document trasformed = out.getDeocument();
XMLOutputter xmlOutputter = new XMLOutputter();
// Output to the I/O stream
InputStream resultIn = new InputStream();
OutputStream resultOut = new OutputStream(resultIn);
xmlOutputter.output(trasformed , sourceOut);
org.apache.xalan.xsltc.compiler.XSLTC xsltc = new org.apache.xalan.xsltc.compiler.XSLTC ();
xsltc.compile(resultIn,className);
}
catch (TransformerException e) {
throw new JDOMException("XSLT Trandformation failed", e);
}
catch(XXXExeption...){..}
}
大致是这样吧,你再试试。