【问题标题】:XSLT Refuses to write DOCTYPE declarationXSLT 拒绝编写 DOCTYPE 声明
【发布时间】:2012-03-21 06:29:25
【问题描述】:

我无法弄清楚我在这里缺少什么。我有一个输出 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 在打开此文档时不断切换到 quirks 模式,而我的 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>

EDIT2:

简而言之,这是我的转换代码:

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

【问题讨论】:

  • 我无法使用提供的xsl:output 在我的转换中重现这个问题——DOCTYPE 编写的。这可能意味着问题出在您未显示的 XML 文档和 XSLT 代码中。请您编辑问题并添加一个小的 XML 文档和一个小的 XSLT 转换,以便任何人都可以将转换应用于 XML 并重现问题吗?

标签: xslt xhtml doctype saxon strict


【解决方案1】:

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

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

【讨论】:

  • 所以问题出在使用 dom4j 的 xmlwriter 上。什么是最好的解决方案?我不确定如何使用 Saxon 的序列化程序。我能找到的唯一序列化程序类位于 net.sf.saxon.s9api 包中,我假设它是用于 v9,但我使用的是 8.7。以某种方式让 dom4j 编写器注入 doctype 是不是有点骇人听闻?不知道该走哪条路……
  • 如果您使用 JAXP 接口运行转换,只需将结果发送到 StreamResult 对象,该对象将使用 XSLT 处理器的序列化程序。
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2017-08-31
  • 2012-11-27
  • 1970-01-01
  • 2014-03-27
  • 2019-06-29
相关资源
最近更新 更多