使用外部XSL样式表呈现本地下载的XML文件的后备策略 [英] Fallback strategy for rendering locally downloaded XML files with external XSL stylesheets
问题描述
在我们的合作内部,我们交换XML文件和我们实验的状态信息。这些XML文件的源是一个中心网站,我们希望文件用XSL样式表呈现。当协作者将XML文件下载到本地计算机并尝试在Firefox中打开文件(或者Safari以外的其他任何东西)时,由于跨域冲突,浏览器拒绝加载现在外部的XSL文件。这种行为是可行的,问题是许多浏览器,而不是显示原始的XML,这将是有帮助的显示一个错误:
Internally in our collaboration we exchange XML files with state information about our experiment. The source of these XML files is a central website where we would like the files to be rendered with a XSL stylesheet. When a collaborator downloads the XML file to her local computer and tries to open the file in Firefox (or anything else than Safari it seems) the browser refuses to load the now external XSL file due to cross-domain conflict. This behaviour is survivable, the problem is that many browsers instead of showing the raw XML which would be helpful shows an error:
Error loading stylesheet: An unknown error has occurred
(805303f4) http://user.web.cern.ch/user/grl_xslt/grl.xsl
有没有办法告诉浏览器,如果XSL文件不可用,显示原始XML或应用另一个CSS基于样式表?
Is there a way to tell the browser that in case the XSL file is unavailable, show the raw XML or apply another CSS based stylesheet?
推荐答案
您可以考虑配置您的服务器以允许访问,例如当我加载一个本地文件的内容
You could consider to configure your server to allow the access, for instance when I load a local file with the contents
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://home.arcor.de/martin.honnen/cdtest/test2012050101.xsl"?>
<root>
<foo>
<bar>test</bar>
</foo>
</root>
它使用远程样式表作为该目录的服务器被配置为允许跨源访问与HTTP标头 Access-Control-Allow-Origin*
。 https://developer.mozilla.org/en/HTTP_access_control
with Firefox 12 it applies the remote stylesheet as the server for that directory is configured to allow cross origin access with the HTTP header Access-Control-Allow-Origin "*"
. https://developer.mozilla.org/en/HTTP_access_control
但是,注意,我得到与Opera不同的结果,它显示文档树,说明没有找到相关的样式表。
Note however that I get different results with Opera, it displays the document tree stating that no associated stylesheet was found.
Chrome拒绝访问到外部样式表为不安全。
And Chrome refuses the access to the external stylesheet as unsafe.
这篇关于使用外部XSL样式表呈现本地下载的XML文件的后备策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!