0

我无法弄清楚我在这里缺少什么。我有一个输出 XML 的 Java Web 应用程序,可以选择将输出转换为 XHTML。我的样式表工作正常,但对于我的生活,我无法获得转换后的输出来编写文档类型。我的 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,看看是否与它有关,但没有运气。有人知道为什么转换器拒绝添加文档类型吗?

编辑:

我只是想建立这个页面(http://mark-allen.net/notes/layout/frames/example.html)。如果我注释掉我的其他模板或应用它们并将我自己的内容放入 div 中并不重要——我不包括示例 XML,因为即使我根本不应用任何模板而只是编写静态 HTML内容,我无法编写文档类型。

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

1 回答 1

3

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

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

于 2012-03-02T20:49:55.713 回答