Hi all, I've been stuck on this for some time now, and perhaps someone here can help
I have my XSLT stylesheet using <xsl:output> to set the doctype of an XML document that doesn't have one. For some reason, that doctype is not being created in Xalan 2.7.1. In 2.6, we had it making the doctype, but ignoring comments (which turned out to be an issue with endDTD never being used). I created a JUnit test that removes any of our application's code and strictly using Xalan with JDOM. I've pasted the code below. The Doctype of the transformed document is always null. I'm thinking this has to do with JDOM and Xalan. Any ideas, suggestions, etc? Thanks! Syl import java.io.StringReader; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import org.jdom.Document; import org.jdom.input.SAXHandler; import org.jdom.output.DOMOutputter; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.ext.LexicalHandler; import junit.framework.TestCase; public class Test extends TestCase { public void testDoctypeCreation() throws Exception { String xmlDoc = "<html><!--This is the first comment--><p>Buddies</p><!--This is the second comment--></html>"; String xslText = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"node() | @*\"><xsl:copy><xsl:apply-templates select=\"@* | node()\"/></xsl:copy></xsl:template><xsl:template match=\"p\"><xsl:copy-of select=\".\"/> yes.</xsl:template></xsl:stylesheet>"; SAXHandler saxHandler = new SAXHandler(); SAXSource xmlSource = new SAXSource(new InputSource(new StringReader(xmlDoc))); SAXSource xsltSource = new SAXSource(new InputSource(new StringReader(xslText))); TransformerFactory factory = new org.apache.xalan.processor.TransformerFactoryImpl(); SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory; TransformerHandler handler = saxFactory.newTransformerHandler(xsltSource); Transformer transformer = handler.getTransformer(); SAXResult result = new SAXResult(); result.setHandler((ContentHandler) saxHandler); result.setLexicalHandler((LexicalHandler) saxHandler); handler.setResult(result); transformer.transform(xmlSource, result); Document transformed = saxHandler.getDocument(); DOMOutputter output = new DOMOutputter(); org.w3c.dom.Document dom = output.output(transformed); assertEquals("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">", dom.getDoctype()); } }