RE: [xsl] Adding text to a transformed file

Subject: RE: [xsl] Adding text to a transformed file
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Mon, 2 Jun 2003 08:54:18 +0100
Two solutions for the price of one:

1. Supply the filename as a parameter to the stylesheet, and output the
extra message from within the stylesheet code, rather than trying to do
it from the calling Java code.

2. Create a FileOutputStream, write your message to it, then wrap the
same FileOutputStream in a StreamResult which you pass as the result
parameter to the transform() method.

Michael Kay


> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Sergio Lorenzo
> Sent: 28 May 2003 13:14
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Adding text to a transformed file
> 
> 
> Hi everyone,
> 
>     I got transform an XML file with an XSL engine, but I 
> would like to write in the output file (pocholo2.html) a line 
> that says : "File transformed from hello.xml", where 
> hello.xml (origin XML file) is a parameter given by this java 
> program. So I would like to pass this parameter to the XSL 
> engine, but I can't get the idea to do it. Maybe, at first 
> time, I can create pocholo2.HTML and add the line:
> 
> fout.writeBytes("File transformed from ",filename);
> 
> and then add the tranformed XML file. I've tried this: create 
> pocholo2.html, open it, write the line, and when I write the 
> transformed XML file, it rewrites over all, obtaining just 
> the XML transformed file, without the above line.
> 
> Any solutions for this problem? Thanks in advance.
> 
> 
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
> import javax.xml.parsers.FactoryConfigurationError;
> import javax.xml.parsers.ParserConfigurationException;
> 
> import org.xml.sax.SAXException;
> import org.xml.sax.SAXParseException;
> import org.w3c.dom.Document;
> import org.w3c.dom.DOMException;
> 
> // For write operation
> import javax.xml.transform.Transformer;
> import javax.xml.transform.TransformerException;
> import javax.xml.transform.TransformerFactory;
> import javax.xml.transform.TransformerConfigurationException;
> import javax.xml.transform.dom.DOMSource;
> import javax.xml.transform.stream.StreamSource;
> import javax.xml.transform.stream.StreamResult;
> 
> import java.io.*;
> 
> 
> public class XML2HTML extends Object{
>   static Document document;
> 
>    public static void main (String argv[]) {
> 
>      if (argv.length != 2) {
>              System.err.println ("Usage: java Stylizer 
> stylesheet xmlfile");
>              System.exit (1);
>          }
> 
>          DocumentBuilderFactory factory =
>              DocumentBuilderFactory.newInstance();
>          //factory.setNamespaceAware(true);
>          //factory.setValidating(true);
> 
>          try {
>            String filename = "pocholo2.html";
>            File stylesheet = new File(argv[0]);
>            System.out.println("1.- XSL stylesheet leido");
>            File datafile   = new File(argv[1]);
>            System.out.println("2.- XML leido");
>            DocumentBuilder builder = factory.newDocumentBuilder();
>            document = builder.parse(datafile);
>            System.out.println("3.- XML parsed");
> 
>            // Use a Transformer for output
>            TransformerFactory tFactory =
>                TransformerFactory.newInstance();
>            StreamSource stylesource = new StreamSource(stylesheet);
>            Transformer transformer = 
> tFactory.newTransformer(stylesource);
>            System.out.println("4.- XSL transformado");
>            DOMSource source = new DOMSource(document);
> 
>            System.out.println("5.- HTML creado");
>            StreamResult result = new StreamResult(filename);
>            transformer.transform(source, result);
> 
>          } catch (TransformerConfigurationException tce) {
>            // Error generated by the parser
>            System.out.println ("\n** Transformer Factory error");
>            System.out.println("   " + tce.getMessage() );
> 
>            // Use the contained exception, if any
>            Throwable x = tce;
>            if (tce.getException() != null)
>              x = tce.getException();
>            x.printStackTrace();
> 
>          } catch (TransformerException te) {
>            // Error generated by the parser
>            System.out.println ("\n** Transformation error");
>            System.out.println("   " + te.getMessage() );
> 
>            // Use the contained exception, if any
>            Throwable x = te;
>            if (te.getException() != null)
>              x = te.getException();
>            x.printStackTrace();
> 
>          } catch (SAXException sxe) {
>            // Error generated by this application
>            // (or a parser-initialization error)
>            Exception  x = sxe;
>            if (sxe.getException() != null)
>              x = sxe.getException();
>            x.printStackTrace();
> 
>          } catch (ParserConfigurationException pce) {
>            // Parser with specified options can't be built
>            pce.printStackTrace();
> 
>          } catch (IOException ioe) {
>            // I/O error
>            ioe.printStackTrace();
>          }
> 
> 
>    }
> }
> 
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread