【问题标题】:Default browser xml stylesheet when referenced xsl stylesheet is not found?未找到引用的 xsl 样式表时的默认浏览器 xml 样式表?
【发布时间】:2012-08-10 09:37:23
【问题描述】:

我希望在浏览器找不到我的样式表时显示原始 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());

}

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

【问题讨论】:

  • 所以没有人想办法解决这个问题?
  • 我不知道是否有可用的回退,因为它取决于每个浏览器的实现。您必须在创建 XML 时动态包含此处理指令。
  • 问题是我不知道生成 XML 时哪个浏览器会读取它。它会迫使我根据请求它的浏览器动态修改 XML...
  • 这是每个 Web 内容的问题 :) 但是当 XSL 不可用时您要求回退,而不是浏览器确定。还是我错了?
  • 你说得对,当 XSL 不可用时,我想要一个后备。

标签: java xml xslt browser


【解决方案1】:

您可以返回 200 和“全部复制”XSL,而不是返回 404。

有点像

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

可以在here找到一个完全复制的 XSL 示例

【讨论】:

    猜你喜欢
    • 2010-09-17
    • 2010-09-07
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2014-12-20
    相关资源
    最近更新 更多