[jdom-interest] 'A DocType cannot be added after the root element' with JDK5

Sunny boy sunny at muellerling.de
Fri Jun 30 12:43:05 PDT 2006


Hi experts,

I'm experiencing some strange behaviour after upgrading from JDK  
1.4.2 to JDK 5.

Here's a little test code to show what my problem is:

===================================================================
import java.io.File;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.stream.*;

import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMSource;
import org.jdom.transform.JDOMResult;
import org.jdom.output.XMLOutputter;
import org.jdom.Document;

public class Transform {

   public static void main(String[] arg) throws  
org.jdom.JDOMException,  
javax.xml.transform.TransformerConfigurationException,  
java.io.IOException, javax.xml.transform.TransformerException  {

     String xmlName  = "bla.xml";
     String xsltName = "bla.xslt";

     File       f         = new File(xmlName);
     SAXBuilder builder   = new SAXBuilder();
     Document   xmlSource = builder.build(f);

     File               xsltFile  = new File(xsltName);
     Source             xslSource = new StreamSource(xsltFile);
     TransformerFactory transFact = TransformerFactory.newInstance();
     Templates          templates = transFact.newTemplates(xslSource);
     Transformer        trans     = templates.newTransformer();

     JDOMSource in  = new JDOMSource(xmlSource);
     JDOMResult res = new JDOMResult();
     trans.transform(in, res);
     Document     r    = res.getDocument();
     XMLOutputter xout = new XMLOutputter();
     xout.output(res.getDocument(), System.out);
   }
}
===================================================================

When I compile and run this little app with JDK 1.4.2 everything  
works fine. When
doing the same with JDK 1.5 it fails with

ERROR:  'A DocType cannot be added after the root element'
Exception in thread "main" javax.xml.transform.TransformerException:  
org.jdom.IllegalAddException: A DocType cannot be added after the  
root element
         at  
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform 
(TransformerImpl.java:647)
         at  
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform 
(TransformerImpl.java:279)
         at Transform.main(Transform.java:35)
Caused by: org.jdom.IllegalAddException: A DocType cannot be added  
after the root element
         at org.jdom.ContentList.documentCanContain(ContentList.java: 
169)
         at org.jdom.ContentList.add(ContentList.java:205)
         at org.jdom.ContentList.add(ContentList.java:140)
         at java.util.AbstractList.add(AbstractList.java:89)
         at org.jdom.Document.addContent(Document.java:319)
         at org.jdom.DefaultJDOMFactory.addContent 
(DefaultJDOMFactory.java:177)
         at org.jdom.input.SAXHandler.startDTD(SAXHandler.java:711)
         at org.jdom.transform.JDOMResult$DocumentBuilder.startDTD 
(JDOMResult.java:592)
         at  
com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.startElement( 
ToXMLSAXHandler.java:657)
         at  
com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.startElement( 
ToXMLSAXHandler.java:508)
         at bla.template$dot$0()
         at bla.applyTemplates()
         at bla.transform()
         at  
com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transfo 
rm(AbstractTranslet.java:594)
         at  
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform 
(TransformerImpl.java:640)
         ... 2 more

The XSLT file 'bla.xslt' for this test case is very simple:
===================================================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" version="1.0" encoding="ISO-8859-1"  
indent="yes"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1- 
strict.dtd"/>

   <xsl:template match="/">
   <html>
     <head>
       <meta name="titel" content="Blabla" />
     </head>
     <body>
       <xsl:value-of select="/bla" />
     </body>
   </html>
   </xsl:template>

</xsl:stylesheet>
===================================================================

The corresponding XML file:
===================================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<bla>a</bla>
===================================================================

Could it be that the XSLT implementation within the JDK has changed  
somehow and that
the JDOMResult is not (yet) "compatible" in some way with JDK 1.5?  
Any ideas how to
overcome this?

I'm experiencing the same behaviour on MacOSX and Linux.

TIA,
Thomas



More information about the jdom-interest mailing list