Hi everyone,
        I posted a question asking about dynamically generating XSL
definitions for XML (versus having a physical XSL file).
I recieved a few emails from people asking me to let them know if I find a
solution. I'm happy to say that I have. Thanks to Steve Maring who works
with me (dont look too far from home for answers huh? :)

Excuse the long code fragment that is to follow:
Note to run this.
        1) Just give it a name of an output file (something.html)
        2) Make sure that you have j2ee.jar, xerces.jar, and xalan.jar in ur
classpath.
I hardcoded the XML and XSL definitions inside the code but you can see how
these can be dynamically generated as Strings from a java class.

Hope this helps :)

package com.nielsenmedia.gmh;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;

public class DynamicXSL
{
    public static void main( String[] args )
    {
        try
        {
            System.setProperty( "javax.xml.parsers.DocumentBuilderFactory",

"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl" );
            System.setProperty( "javax.xml.transformer.TransformerFactory",

"org.apache.xalan.xsltc.trax.TransformerFactoryImpl" );

            FileOutputStream fos = new FileOutputStream( args[0] );

            DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            String xml =
                "<?xml version=\"1.0\"?>"+
                "<page>"+
                "   <title>Hello World!</title>"+
                "   <content>"+
                "       <paragraph>This was generated with dynamic XML and
dynamic XSL!!</paragraph>"+
                "   </content>"+
                "</page>";

            Document document = docBuilder.parse( new ByteArrayInputStream(
xml.getBytes() ) );

            TransformerFactory transFactory =
TransformerFactory.newInstance();
            String xsl =
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
                "<xsl:stylesheet version=\"1.0\""+
                " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""+
                " xmlns:fo=\"http://www.w3.org/1999/XSL/Format\";>"+
                "<xsl:output method=\"html\"/>"+

                "<xsl:template match=\"page\">"+
                "  <html>"+
                "    <head>"+
                "      <title><xsl:value-of select=\"title\"/></title>"+
                "    </head>"+
                "    <body bgcolor=\"#ffffff\">"+
                "       <center>"+
                "         <font
color=\"red\"><xsl:apply-templates/></font>"+
                "       </center>"+
                "    </body>"+
                "  </html>"+
                "</xsl:template>"+
                "</xsl:stylesheet>";
            Transformer transformer = transFactory.newTransformer(
                new StreamSource(
                new ByteArrayInputStream( xsl.getBytes() ) ) );
            transformer.setOutputProperty( "method", "html" );
            transformer.setOutputProperty( "media-type", "text/plain" );
            transformer.setOutputProperty( "version", "1.0" );
            transformer.setOutputProperty( "indent", "yes" );
            transformer.setOutputProperty( "encoding", "UTF-8" );
            transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount";, "4" );
            transformer.transform( new DOMSource( document ), new
StreamResult( fos ) );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
}

[EMAIL PROTECTED]
727-738-3000 x6549

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to