Use output of XSL transformation as new XSL stylesheet

Discussion in 'XML' started by barney.b@iname.com, Jan 16, 2006.

  1. Guest

    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 a NullPointerException. I'm not 100% sure exactly what
    is going on here, so if someone could help I'd be grateful.

    I've been working from the examples from Sun and Apache Xalan, 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
    , Jan 16, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Conmackie
    Replies:
    4
    Views:
    715
    Mike Conmackie
    Apr 29, 2004
  2. =?ISO-8859-1?Q?J=F8rn_Tommy_Kinder=E5s?=

    Stylesheet referanse i stylesheet

    =?ISO-8859-1?Q?J=F8rn_Tommy_Kinder=E5s?=, Jul 3, 2004, in forum: XML
    Replies:
    3
    Views:
    562
    =?ISO-8859-1?Q?J=F8rn_Tommy_Kinder=E5s?=
    Jul 4, 2004
  3. Thomas Sommer
    Replies:
    0
    Views:
    422
    Thomas Sommer
    Nov 25, 2004
  4. MC
    Replies:
    1
    Views:
    7,232
    David Carlisle
    Mar 24, 2005
  5. David Blickstein
    Replies:
    3
    Views:
    1,025
    David Blickstein
    Aug 2, 2005
Loading...

Share This Page