开发者

Can I do XSL transformation without explicitly provided XSL file?

I am using Transformer to perform XSL transformation from XML to XHTML:

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
// ...
Transformer transformer = TransformerFactory.newInstance()
  .newTransformer(/* xsl */);
transformer.transform(new StreamSource(xml), new StreamResult(xhtml));

In this code I should explicitly provide XSL file. In my situation I don't know which file should be used. Ins开发者_如何学Ctead, I want the transformer to get this information from <?xsl-stylesheet?> processing instruction in the original XML. Is it possible to do with JDK6 and Saxon?


Xalan can do that, check this. Probably Saxon as well as it is method of javax.xml.transform.TransformerFactory: getAssociatedStylesheet


Thanks to @JustYo suggestion I found it's working well under Saxon.

System.setProperty("javax.xml.transform.TransformerFactory", 
    "net.sf.saxon.TransformerFactoryImpl");

StreamSource xmlSource = new StreamSource(xml);
TransformerFactory factory = TransformerFactory.newInstance();
Source xslSource = factory.getAssociatedStylesheet(xmlSource, null, null, null);
Transformer transformer = factory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult(xhtml));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜