我無法弄清楚我在這裏失蹤的內容。我有一個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);
我不能在我的轉換中使用提供的'xsl:output'重現這個問題 - DOCTYPE *被寫入。這可能意味着問題出現在您未顯示的XML文檔和XSLT代碼中。您能否編輯該問題並添加一個小的XML文檔和一個小的XSLT轉換,以便任何人都可以將轉換應用於XML並重現問題? – 2012-03-02 18:46:53