XSLT拒绝编写DOCTYPE声明



我想不出这里缺少了什么。我有一个Java web应用程序,它可以输出XML,并可以选择将输出转换为XHTML。我的样式表运行良好,但就我的生活而言,我无法获得转换后的输出来编写doctype。xsl:stylesheet元素下面的第一个子元素是:

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

即使我将输出写入System.out,我也可以验证它不会将doctype声明放在顶部。不幸的是,IE9在打开此文档时一直切换到怪癖模式,而我的CSS依赖于标准模式。

我开始使用Saxon 9.1.0.8,然后恢复到8.7,看看这是否与它有关,但没有运气。有人知道为什么转换器拒绝添加doctype吗?

编辑:

我只是想建立这个页面(http://mark-allen.net/notes/layout/frames/example.html)。无论我是注释掉其他模板,还是应用它们并将自己的内容放在div中,这都无关紧要——我不包括示例XML,因为即使我根本不应用任何模板,只编写静态HTML内容,我也无法用它来编写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>

第2版:

以下是我的转换代码:

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);

最可能的解释是样式表输出没有使用Saxon序列化程序进行序列化。例如,您可能正在将输出写入DOM,然后使用DOM的序列化程序来生成词法XML。

然而,这只是一个猜测——您还没有提供任何关于如何运行转换的信息。

最新更新