2012-08-10 80 views
1

我想在我的樣式表沒有被瀏覽器找到時顯示原始XML,有可能嗎?未找到引用xsl樣式表時的默認瀏覽器xml樣式表?

下面我把我的XML文檔的標題。

<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="./my_sheet.xsl" title="relativeRef" alternate="YES"?> 

當我的服務器上找不到樣式表我將返回404錯誤,但是這使得瀏覽器停止並顯示錯誤消息的XML解析器。我該怎麼辦?這是服務樣式表的servlet的JAVA代碼。

@Override 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws ServletException, IOException {  

    edamisUtils.log("Serving my_sheet.xsl"); 

    //If we could not load my_sheet we throw a 404 error. 
    if(my_sheet== null){ 
     edamisUtils.debug(5, "Did not find the my_sheet.xsl, sending a 404 status response."); 
     resp.sendError(HttpServletResponse.SC_NOT_FOUND); 
     return; 
    } 

    //Set the MIME type of the response to text/xsl 
    resp.setContentType("text/xsl"); 
    OutputStream os = resp.getOutputStream(); 
    os.write(my_sheet.array()); 

} 

謝謝大家,祝你有美好的一天!

+0

所以沒有人有一個想法來解決這個問題? – Abbadon 2012-08-13 11:12:17

+0

我不確定是否有可用的回退,因爲它取決於每個瀏覽器的實現。創建XML時,必須動態包含此處理指令。 – kernel 2012-08-13 20:24:15

+0

問題是,我不知道何時生成瀏覽器將讀取它的XML。它會迫使我根據請求它的瀏覽器來動態地修改XML ... – Abbadon 2012-08-14 08:17:38

回答

0

而不是返回404,您可以返回200和「全部拷貝」XSL。

喜歡的東西

if(my_sheet == null){ 
    copy the copy_all XSL in the response output stream; 
} 

副本,所有的XSL例子可以發現here