XSLT 在 Web 浏览器中不起作用 [英] XSLT not working in web browser

查看:49
本文介绍了XSLT 在 Web 浏览器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 XML 样式的 XSLT 文件.XSLT 可通过 URL (http://someurl/somefile.xsl) 毫无问题地访问.

I have an XSLT file for styles in XML. The XSLT is accessible via a URL (http://someurl/somefile.xsl) without problems.

当我将相同的 URL 插入到 xml-stylesheet 处理指令中时,它只在浏览器(FF、IE)中呈现纯文本,

When I insert the same URL into an xml-stylesheet processing instruction, it only renders plain text in browsers (FF, IE),

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://someurl/somefile.xsl"?>
<rootElement>...</rootElement>

但是当我使用本地文件路径(文件下载到与 XML 文件相同的文件夹)时,它就像一个魅力:

but when I use a local file path (file downloaded to same folder as the XML file), it works like a charm:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="somefile.xsl"?>
<rootElement>...</rootElement>

为什么?

推荐答案

在 Web 浏览器中运行 XSLT

在浏览器中运行 XSLT 会受到一些限制:

Running XSLT in a Web Browser

Running XSLT in the browser is subject to some limitations:

  • XSLT 2.0 不受任何主要网络浏览器的支持.

  • XSLT 2.0 is not supported by any of the major web browsers.

浏览器安全模型在 XSLT 处理方面有所不同.

Browser security models differ regarding XSLT processing.

  • 跨域限制通常要求 XSLT 从与 XML 相同的来源加载.(在这种情况下,这似乎在咬你.)

  • Cross-domain restrictions will often require that the XSLT load from the same origin as the the XML. (This appears to be biting you in this case.)

Chrome 确实不允许 本地加载的 XSLT 运行(即使 XML 是本地加载的).这在开发过程中可能会很烦人.

Chrome does not allow locally loaded XSLT to run (even when the XML is locally loaded). This can be annoying during development.

出于这些原因,XSLT 更常在服务器上或以批处理模式运行,而不是在浏览器中运行.

For these reasons, XSLT is more often run on the server or in batch mode rather than in the browser.

如果您希望在浏览器中运行 XSLT 并使其与 Chrome、Firefox 和 IE 兼容,您必须

  1. 仅使用 XSLT 1.0, XSLT 2.0.
  2. 在 XML 文件中使用 xml-stylesheet 处理指令,就像将 XSLT 文件与 XML 文件链接起来所做的那样:

  1. Use XSLT 1.0 only, not XSLT 2.0.
  2. Use an xml-stylesheet processing instruction in the XML file as you've done to link the XSLT file with the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://origin-domain/path/to/file.xsl"?>
<rootElement>...</rootElement>

  • 从服务器而非本地文件提供 XSLT.
  • 确保 XSLT 来自与 XML 相同的域.
  • 最后,请务必检查浏览器控制台是否有任何错误消息.例如,当找不到 XSLT 时,IE 会显示以下内容:

    Finally, be sure to check the browser console for any error messages. For example, here's what IE shows when the XSLT cannot be located:

    这篇关于XSLT 在 Web 浏览器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆