2012-03-02 82 views
0

我无法弄清楚我在这里失踪的内容。我有一个Java Web应用程序,它可以输出XML,并将输出转换为XHTML。我的样式表工作正常,但在我的生活中,我无法获得转换后的输出来编写doctype。下面我XSL的第一个孩子:stylesheet元素是:XSLT拒绝写DOCTYPE声明

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

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

我不能在我的转换中使用提供的'xsl:output'重现这个问题 - DOCTYPE *被写入。这可能意味着问题出现在您未显示的XML文档和XSLT代码中。您能否编辑该问题并添加一个小的XML文档和一个小的XSLT转换,以便任何人都可以将转换应用于XML并重现问题? – 2012-03-02 18:46:53

回答

3

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

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

+0

所以问题是使用dom4j的xmlwriter。什么是最好的解决方案?我不确定如何使用撒克逊的序列化程序。我唯一可以找到的序列化类是在net.sf.saxon.s9api包中,我假设它是用于v9的,但我使用的是8.7。是否hach-ish以某种方式使dom4j作家注入了文档类型?不确定要走哪条路...... – Bal 2012-03-03 16:30:43

+0

如果使用JAXP接口运行转换,只需将结果发送到StreamResult对象,并使用XSLT处理器的序列化程序。 – 2012-03-05 21:35:53