I can describe generally how I did it.

First, I have an XML file that represents an extract of a database.  This isn't
simple rows and columns.  It is a dump of a workflow's entire history.  Lots of
different types of data, one-to-many relationships, etc.  All nicely packaged in
an XML file.

Second.  I studied Bruno's example XML to see the tags, attributes, etc. that
the XML-to-PDF conversion used.

Third.  I worked up a stylesheet-transform that would transform my raw xml file
into an xml acceptable to the xml-pdf converter.

Fourth.  I used the code #1 below to apply the transform to my original xml
file.  Just standard Xerces xlst stuff.

Fifth.  I then used the new XML file which was itext compliant in an itext
program (ie, it used the SAXiTextHandler() object) to create both HTML and PDF
in one fell swoop.  This is also below as code #2.

Just for samples.. here is the first two lines of my raw xml file:

<ProcessHistory>
<recordedOn>Tue Jan 01 09:58:47 EST 2002</recordedOn>

Here is the first few lines of the XSL file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    version="1.0">

<!-- Root Processing begins here for the first chapter -->
<xsl:template match="/ProcessHistory">
<itext>
<chapter numberdepth="1" depth="1" indent="0.0">
<title leading="36.0" align="Default" font="Helvetica"
    size="24.0" style="normal" red="255" green="0" blue="0">
    <chunk font="Helvetica" size="24.0" style="normal"
    red="255" green="0" blue="0">
Workflow Historical Record
    </chunk>
</title>

<paragraph>
Recorded on:
<xsl:value-of select="./recordedOn/text()" />
</paragraph>

Then finally, here are the first few lines of the transformed XML, ready for
input into the itext code:
<?xml version="1.0" encoding="UTF-8"?>
<itext><chapter indent="0.0" depth="1" numberdepth="1">
 <title blue="0" green="0" red="255" style="normal" size="24.0" 
  font="Helvetica" align="Default" leading="36.0">
    <chunk blue="0" green="0" red="255" style="normal" 
      size="24.0" font="Helvetica">
Workflow Historical Record
    </chunk>
 </title>
<paragraph>
Recorded on:
Tue Jan 15 10:42:29 EST 2002
</paragraph>

--------- code #1 --------------
import java.io.FileOutputStream;
import java.io.IOException;

import org.xml.sax.Parser;
import org.xml.sax.helpers.ParserFactory;
import org.apache.xalan.xslt.*;;

public class xslt {

private static final String PARSER = "org.apache.xerces.parsers.SAXParser";

public static void main(String[] args) {
  if ( args.length != 3 ) {
    System.out.println("Usage: java xslt input.xml my.xls output.xml");
    System.exit(-1);
  }
  try {
    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    XSLTProcessor processor =
      XSLTProcessorFactory.getProcessor();
      //XSLTProcessorFactory.getProcessor( new XercesLiaison() );
    XSLTInputSource xslfile = new XSLTInputSource(args[1]);
    XSLTInputSource xmlfile = new XSLTInputSource(args[0]);

    XSLTResultTarget out    = new XSLTResultTarget(args[2]);
    processor.process(xmlfile, xslfile, out);
  }
  catch(Exception e) {
    e.printStackTrace();
    System.err.println(e.getMessage());
  }
}
}


------------- code #2 --------------
import java.io.FileOutputStream;
import java.io.IOException;
import org.xml.sax.Parser;
import org.xml.sax.helpers.ParserFactory;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.xml.*;

public class x2p {
private static final String PARSER = "org.apache.xerces.parsers.SAXParser";
public static void main(String[] args) {
  if ( args.length != 1 ) {
    System.out.println("Usage: java x2p basename");
    System.out.println("basename generates basename.xml/pdf/html");
    System.exit(-1);
  }
  // step 1: creation of a document-object
  Document document = new Document();
  try {
    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    PdfWriter.getInstance(document, new FileOutputStream(args[0]+".pdf"));
    HtmlWriter.getInstance(document, new FileOutputStream(args[0]+".html"));

    // step 3: we create a parser and set the document handler
    Parser parser = ParserFactory.makeParser(PARSER);
    //SAXParserFactory spf = SAXParserFactory.newInstance();
    //SAXParser parser = spf.newSAXParser();
    parser.setDocumentHandler(new SAXiTextHandler(document));

    // step 4: we parse the document
    parser.parse(args[0]+".xml");
  }
  catch(Exception e) {
    e.printStackTrace();
    System.err.println(e.getMessage());
  }
}
}

-----Original Message-----
From: lihui [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 04, 2002 5:08 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: xml + xsl


Hi new cecil,

Seems you succeeded generating a iText xml with generic xml plus xlt. Could
you share some more details on that? Would be perfect if can show some
examples.

Thank you.

Lihui


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to