[jdom-interest] Problems using JDOM and XSLT
Yuri de Wit
ydwit at worldnet.att.net
Mon Apr 16 13:00:21 PDT 2001
I have the latest JDOM source plus Xalan 2.0.1 and the XsltTranformer helper
class provided ealier in the mailing list, but I am not able to do a:
JDOM -> XSLT-> JDOM transformation
I am getting the following exception in Xalan:
javax.xml.transform.TransformerException
at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.j
ava:1183)
at
org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2942)
at java.lang.Thread.run(Thread.java:484) ---------
java.util.EmptyStackException
at
org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(Transformer
Impl.java:2038)
at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.j
ava:1219)
at
org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2942)
at java.lang.Thread.run(Thread.java:484)
ANY IDEAS??
Thanks,
Stylesheet:
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='BEGIN'>
<xsl:element name='WHATEVER'>DATA</xsl:element>
</xsl:template>
</xsl:stylesheet>
Code:
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXHandler;
import org.jdom.output.SAXOutputter;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
/**
* Helper class with one static method to allow clients to transform JDOM
* documents using XSLT.
*/
public class XsltTransformer
{
private static SAXOutputter saxOutputter;
private static SAXTransformerFactory saxTFactory;
private static TemplatesHandler templatesHandler;
private static XMLReader reader;
static{
try{
// don't forget to set the ContentHandler before using
// the outputter!
saxOutputter = new SAXOutputter( null );
saxTFactory = (SAXTransformerFactory)
TransformerFactory.newInstance();
templatesHandler = saxTFactory.newTemplatesHandler();
reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler( templatesHandler );
}
catch( Exception e ){
e.printStackTrace();
//System.exit( -1 );
}
}
/**
* Transforms a JDOM document into another JDOM document using the given
* stylesheet.
*/
public static Document transform( Document sourceDoc, String
stylesheet )
throws SAXException, IOException,
TransformerConfigurationException, JDOMException
{
reader.parse( stylesheet );
Templates templates = templatesHandler.getTemplates();
TransformerHandler handler = saxTFactory.newTransformerHandler(
templates );
Document targetDoc = new Document((Element)null);
// had to turn SAXHandler into a public class to use it here:
handler.setResult( new SAXResult( new
SAXHandler( targetDoc ) ) );
saxOutputter.setContentHandler( handler );
saxOutputter.output( sourceDoc );
return targetDoc;
}
Yuri
More information about the jdom-interest
mailing list