You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Barney Barumba <ba...@iname.com> on 2006/01/27 10:44:56 UTC

Using TemplatesHandler as a SAXResult

Hi,

I'm trying to pre-process an XSL stylesheet with another stylesheet,
before using the result to transform an XML document. i.e.:

     stylesheet(xsl) --> preprocessor(xsl) --> temp(xsl)
     input(xml) --> temp(xsl) --> output(xml)

but I'm getting an error when I try. I'm not 100% sure exactly what
is going on here, so if someone could help I'd be grateful.

I've been working through the Xalan examples, and so
far have the following:

     File input = new File("input.xml");
     File stylesheet = new File("stylesheet.xsl");
     File preprocessor = new File("preprocess.xsl");

     SAXTransformerFactory stf =
(SAXTransformerFactory)TransformerFactory.newInstance();

     // parse preprocessor stylesheet into templates handler
     TemplatesHandler tmpltHand = stf.newTemplatesHandler();
     XMLReader reader = XMLReaderFactory.createXMLReader();
     reader.setContentHandler(tmpltHand);
     reader.parse(new InputSource(new FileInputStream(preprocessor)));

     // create transformer handler based on templates handler
     Templates templates = tmpltHand.getTemplates();
     TransformerHandler transHand =
stf.newTransformerHandler(templates);

     // parse page stylesheet through proprocessor
     reader.setContentHandler(transHand);
     reader.setProperty("http://xml.org/sax/properties/lexical-handler",
transHand);

     transHand.setResult(new StreamResult(System.out));
     reader.parse(new InputSource(new FileInputStream(stylesheet)));

This all works fine, and displays the modified stylesheet. It's when I
try to use this stylesheet it all goes wrong. I thought I could just
redirect the output of the last stage, via a SAXResult. into a new
TemplatesContentHandler, and then use this to transform the actual
input.

replace:
     transHand.setResult(new StreamResult(System.out));

with:
     TemplatesHandler tmpltHand2 = stf.newTemplatesHandler();
     transHand.setResult(new SAXResult(tmpltHand2));

However, when I run this I get a NullPointerException in
TransformerHandlerImpl.endDocument.

Can anybody see what is wrong here, or suggest a better way of doing
this?

Cheers,

Barney



-- 
___________________________________________________
Play 100s of games for FREE! http://games.mail.com/


Re: Using TemplatesHandler as a SAXResult

Posted by Erin Harris <eh...@ca.ibm.com>.
Hi,

Which version of Xalan are you using?

I tried the following with Xalan 2.7.0 and it seemed to work (though 
admittedly I used an identity transform for the preprocessor.xsl):

      SAXTransformerFactory stf = 
(SAXTransformerFactory)TransformerFactory.newInstance();

      TemplatesHandler tmpltHand = stf.newTemplatesHandler();
      XMLReader reader = XMLReaderFactory.createXMLReader();
      reader.setContentHandler(tmpltHand);
      reader.parse("identity.xsl");

      Templates templates = tmpltHand.getTemplates();
      TransformerHandler transHand = stf.newTransformerHandler(templates);
      reader.setContentHandler(transHand); 
      reader.setProperty("http://xml.org/sax/properties/lexical-handler", 
transHand);
 
 
      TemplatesHandler tmpltHand2 = stf.newTemplatesHandler();
      transHand.setResult(new SAXResult(tmpltHand2));
      reader.parse("simple.xsl");
 
      Templates templates2 = tmpltHand2.getTemplates();
      TransformerHandler transHand2 = 
stf.newTransformerHandler(templates2);
      reader.setContentHandler(transHand2);
      reader.setProperty("http://xml.org/sax/properties/lexical-handler", 
transHand2);

      transHand2.setResult(new StreamResult(System.out));
      reader.parse("simple.xml");
 
Thanks,

Erin Harris





"Barney Barumba" <ba...@iname.com> 
01/27/2006 04:44 AM

To
xalan-j-users@xml.apache.org
cc

Subject
Using TemplatesHandler as a SAXResult






Hi,

I'm trying to pre-process an XSL stylesheet with another stylesheet,
before using the result to transform an XML document. i.e.:

     stylesheet(xsl) --> preprocessor(xsl) --> temp(xsl)
     input(xml) --> temp(xsl) --> output(xml)

but I'm getting an error when I try. I'm not 100% sure exactly what
is going on here, so if someone could help I'd be grateful.

I've been working through the Xalan examples, and so
far have the following:

     File input = new File("input.xml");
     File stylesheet = new File("stylesheet.xsl");
     File preprocessor = new File("preprocess.xsl");

     SAXTransformerFactory stf =
(SAXTransformerFactory)TransformerFactory.newInstance();

     // parse preprocessor stylesheet into templates handler
     TemplatesHandler tmpltHand = stf.newTemplatesHandler();
     XMLReader reader = XMLReaderFactory.createXMLReader();
     reader.setContentHandler(tmpltHand);
     reader.parse(new InputSource(new FileInputStream(preprocessor)));

     // create transformer handler based on templates handler
     Templates templates = tmpltHand.getTemplates();
     TransformerHandler transHand =
stf.newTransformerHandler(templates);

     // parse page stylesheet through proprocessor
     reader.setContentHandler(transHand);
     reader.setProperty("http://xml.org/sax/properties/lexical-handler",
transHand);

     transHand.setResult(new StreamResult(System.out));
     reader.parse(new InputSource(new FileInputStream(stylesheet)));

This all works fine, and displays the modified stylesheet. It's when I
try to use this stylesheet it all goes wrong. I thought I could just
redirect the output of the last stage, via a SAXResult. into a new
TemplatesContentHandler, and then use this to transform the actual
input.

replace:
     transHand.setResult(new StreamResult(System.out));

with:
     TemplatesHandler tmpltHand2 = stf.newTemplatesHandler();
     transHand.setResult(new SAXResult(tmpltHand2));

However, when I run this I get a NullPointerException in
TransformerHandlerImpl.endDocument.

Can anybody see what is wrong here, or suggest a better way of doing
this?

Cheers,

Barney



-- 
___________________________________________________
Play 100s of games for FREE! http://games.mail.com/