且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XSLT 在 Web 浏览器中不起作用

更新时间:2023-01-24 12:13:52

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