Bal
Bal

Reputation: 2087

XSLT Refuses to write DOCTYPE declaration

I can't figure out what I'm missing here. I have a Java web app that outputs XML with the option to transform the output to XHTML. My stylesheet works fine, but for the life of me, I cannot get the transformed output to write the doctype. The first child below my xsl:stylesheet element is:

<xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />

Even if I write the output to System.out, I can verify that it will not put a doctype declaration at the top. Unfortunately IE9 keeps switching itself to quirks mode when opening this document and my CSS relies on standards mode.

I started out using Saxon 9.1.0.8 and just reverted to 8.7 to see if that had anything to do with it, but no luck. Anybody have any idea why the transformer refuses to add the doctype?

EDIT:

I'm just trying to build this page (http://mark-allen.net/notes/layout/frames/example.html). It doesn't matter if I comment out my other templates or apply them and put my own content in the divs -- I'm not including sample XML because even when I don't apply any templates at all and just write the static HTML content, I can't get it to write the doctype.

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

    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />

    <xsl:param name="restUrl" />
    <xsl:param name="resourcesUrl" />

    <xsl:variable name="space"><xsl:text> </xsl:text></xsl:variable>

    <xsl:template match="sos:Capabilities"> 
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>Capabilities</title>
                  <style type="text/css">
    body {
        margin:0;
        padding:0 10px 0 10px;
        height:100%;
        overflow-y:auto;
    }

    #header {
        display:block;
        top:0px;
        left:0px;
        width:100%;
        height: 100px;
        position:fixed;
        clear: both;
        border-bottom : 2px solid #cccccc;
                background-color: black;
    }

    #header p.description {
            color: #FF0000;
    }

    #navigation {
        display:block;
        top:120px;
        left:0px;
        width:380px;
        height: 100%;
        position:fixed;
        border:1px solid #00FF00;
    }

    #navigation p.description {
            color: #00FF00;
    }

    #content {
        margin:100px 0px 60px 380px;
        display:block;
        padding:10px;
        border:1px solid #0000FF;
    }

    #content p.description {
        color: #0000FF;
    }

         #footer {
                position: fixed;
                width: 100%;
                height: 60px;
                right: 0;
                bottom: 0;

                border-top : 2px solid #cccccc;
                background-color: black;
                background-image: url("../images/saic.gif");
                background-position: right bottom;
                background-repeat: no-repeat;
         }

    * html #header {position:absolute;}
    * html #navigation {position:absolute;}

            </style>
            </head>
            <body>
                <div id="header">
                    This is my header
                </div>
                <div id="navigation"> 
                     Navigation
                </div>
                <div id="content">
                    <p>lots of random text just to test</p>
                 </div>
                 <div id="footer">
                     footer
                 </div>    
              </body>
         </html>
    </xsl:template>
</xsl:stylesheet>

EDIT2:

Here is my transform code in a nutshell:

System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
            org.dom4j.io.DocumentSource source = new DocumentSource(queryResponseDocument);
            Source xsltSource = new StreamSource(new File(contextPath, xsltFileName));
            org.dom4j.io.DocumentResult result = new DocumentResult();

            TransformerFactory transFact = TransformerFactory.newInstance();
            Transformer trans = transFact.newTransformer(xsltSource);
            trans.transform(source, result);
            transformedQueryResponse = result.getDocument();
            response.setContentType(mimeType);
            org.dom4j.io.OutputFormat format = OutputFormat.createPrettyPrint();
            org.dom4j.io.XMLWriter writer = new XMLWriter(response.getOutputStream(), format);

Upvotes: 0

Views: 2148

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

The most likely explanation is that the stylesheet output is not being serialized using the Saxon serializer. For example, you might be writing the output to a DOM, and then using the DOM's serializer to produce the lexical XML.

That's just a guess, however - you haven't provided any information about how the transformation is being run.

Upvotes: 3

Related Questions