Hi,
I have been trying to send SAX events to TransformerHandler to transform XML. I pass a templates object while creating the TransformerHandler. I am using xalan 2.5 along with xercesImpl and xml-apis.jar files that ship with xalan for my
test. I am finding that the style sheet does not match any of the XML elements. However, a transformation using transformer.transform() works and gives correct results.
I have experimented with all xalan versions (2.1 through 2.6) and found that the same code that uses TransformerHandler works fine when using xalan 2.1. After that, it does not seem to be working. Is this a known bug ? How do I get around it ?
I am attaching a test program I have used for my testing .
Thanks in advance for your help.
Regards,
Sankar
---------------------------------------------------------------------
//Source code for
Java Program
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
/**
* Program to test Transformation using TransformerHandler that uses Templates
*/
public class SimpleXSLProcessor
{
public static void main(
String[] args) throws Exception
{
// Specify XML Name,
if (args.length == 0)
{
System.err.println(
"Usage: java "
+ SimpleXSLProcessor.class.getName()
+ " [useTransformerHandler] [xsltFile] <xmlFile>");
System.err.println(" - useTransformerHandler is required");
System.err.println(" - xsltFile is required");
System.err.println(" - xmlFile is optional if useTransformerHandler is 'Y' ");
System.exit(1);
}
boolean useTransformerHandler = args[0].equalsIgnoreCase("Y") ? true: false;
String xsltFileName = (args.length > 1) ? args[1] : null;
String xmlFileName = (args.length > 2) ? args[2] : null;
TransformerFactory transFact = TransformerFactory.newInstance();
if (transFact.getFeature(SAXTransformerFactory.FEATURE))
{
SAXTransformerFactory saxTransFact =
(SAXTransformerFactory)transFact;
TransformerHandler transHand = null;
if (!useTransformerHandler)
{
System.out.println(" Transformation Output Using Transformer :");
Transformer transformer = saxTransFact.newTransformer( new StreamSource(new File(xsltFileName)));
transformer.transform(new StreamSource(new File(xmlFileName)),new StreamResult(System.out));
}
else
{
System.out.println(" Transformation Output Using TransformerHandler :");
javax.xml.transform.Templates templates = saxTransFact.newTemplates( new StreamSource(new File(xsltFileName)) );
transHand = saxTransFact.newTransformerHandler(templates);
// set the destination for the XSLT transformation
transHand.setResult(new StreamResult(System.out));
Generate(transHand);
}
}
else
{
System.err.println("SAXTransformerFactory is not supported.");
System.exit(1);
}
}
//Create SAX events to be sent to content handler of TransformerHandler
static void Generate(org.xml.sax.ContentHandler aContent) throws Exception
{
aContent.startDocument();
// Now we can actually write the date out.
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "", "state", "CDATA", "NJ");
atts.addAttribute("", "", "zip", "CDATA", "07753");
atts.addAttribute("", "", "sicname", "CDATA", "Veterinary Services, Animal Specialties");
atts.addAttribute("", "", "siccode", "CDATA", "0742");
atts.addAttribute("", "", "rep", "CDATA", "sankar");
atts.addAttribute("", "", "pdate", "CDATA", "April 9, 2004" );
atts.addAttribute("", "", "edate", "CDATA", "May 1, 2004");
atts.addAttribute("", "", "vdate", "CDATA", "May, 2004");
atts.addAttribute("", "", "name", "CDATA", "TESTPDF");
atts.addAttribute("", "", "timestamp", "CDATA", "April 9, 2004 10:19:33 AM");
atts.addAttribute("", "", "lives", "CDATA", "8");
atts.addAttribute("", "", "insured", "CDATA", "8");
atts.addAttribute("", "", "logo", "CDATA", "C:\\logo.gif");
aContent.startElement("", "", "root", atts);
aContent.startElement("", "", "cover", new AttributesImpl());
aContent.endElement("", "", "cover");
aContent.endElement("", "", "root");
aContent.endDocument();
}
}
------------------------------------------------------------------
// XSL File
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:template match='/root'>
Found the root
<xsl:apply-templates/>
</xsl:template>
<xsl:template match='cover'>
Found the cover
</xsl:template>
</xsl:stylesheet>
-------------------------------------------------------------------
// XML File
<?xml version="1.0" encoding="UTF-8"?>
<root state="NJ" zip="07753" sicname="Veterinary Services, Animal Specialties" siccode="0742" rep="sankar" pdate="April 9, 2004" edate="May 1, 2004" vdate="May, 2004" name="testpdf" timestamp="April 9, 2004 10:19:33 AM" lives="8" insured="8" logo="logo.gif"><cover/></root>
---------------------------------------------------
//Output from my program
C:\Sankar\Programs\XMLTest>java -classpath .;xalan.jar;xml-apis.jar;xercesImpl.jar SimpleXSLProcessor Y identity.xsl
Transformation Output Using TransformerHandler :
<?xml version="1.0" encoding="UTF-8"?>
C:\Sankar\Programs\XMLTest>java -classpath .;xalan.jar;xml-apis.jar;xercesImpl.jar SimpleXSLProcessor N identity.xsl source.xml
Transformation Output Using Transformer :
<?xml version="1.0" encoding="UTF-8"?>
Found the root
Found the cover