从data:text/xml调用XSLT文件 [英] Call XSLT file from data:text/xml

查看:64
本文介绍了从data:text/xml调用XSLT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

祝大家有美好的一天

我正在尝试使用我从REST获得的data:text/xml渲染的XML调用XSLT文件.最基本的尝试是使用以下代码行:

  window.open('data:text/xml,'+ encodeURIComponent(responseData)); 

我的另一种尝试是在一个模态iframe(使用角度材质)中打开它,并使用xml数据设置src,这两个测试均使用URL打开XML,但出现此错误:

加载样式表时出错:未知错误(805303f4)

我已经在firefox和chrome中对其进行了测试,并且通过控制台中的错误详细信息,chrome更清晰了:

从具有URL的框架 data:text/xml的不安全尝试加载URL route-to-xsl.xsl ... 域,协议和端口必须匹配.

我尝试使用包括相同协议,域等在内的样式表的绝对路径,但错误仍然存​​在(在iframe中或新标签页选项中).

任何帮助,谢谢.

解决方案

由于这是Firefox或Chrome等网络浏览器中的客户端Javascript,我建议使用 XSLTProcessor 来执行XSLT转换,您可以使用 XMLHttpRequest 提取XSLT样式表,使用 DOMParser 解析 responseData ,然后可以使用 XSLTProcessor 为转型.

除非您将XSLT本身作为数据嵌入,否则我认为您不会让浏览器执行在 data URL中引用的XSLT:

  var encodeXslt ='data:application/xml,'+ encodeURIComponent(document.getElementById('xslt').textContent);var xmlCode = document.getElementById('xml').textContent;var pi ='<?xml-stylesheet type ="text/xsl" href ='+ encodeXslt +'"?>';var encodeXml ='data:application/xml,'+ encodeURIComponent(pi + xmlCode);window.frames.xmlDisplay.location.href = encodeXml;  

 <脚本id ="xslt" type ="application/xml + xslt">< xsl:stylesheet xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" version ="1.0">< xsl:output method ="html" indent ="yes"/>< xsl:template match ="list">< ul>< xsl:apply-templates/></ul></xsl:template>< xsl:template match ="item">< li>< xsl:apply-templates/></li></xsl:template></xsl:stylesheet></script>< script id ="xml" type ="application/xml">< list>< item> foo</item>< item> bar</item></list></script>< iframe名称="xmlDisplay" width ="100%" height ="300"></iframe>  

但是,似乎只有Mozilla吞并尝试并应用XSLT,Chrome仍然抱怨这种不安全的尝试.因此,我认为在XSLTProcessor中将输入作为Javascript字符串输入的情况下,实现任何XSLT转换都是更好,更可移植的.

Have a nice day everybody

i'm trying to call a XSLT file from an XML that i'm rendering using data:text/xml, obtained from a REST. The most basic attempt is to use this line of code:

window.open( 'data:text/xml,' + encodeURIComponent( responseData ) );

another attempt of mine is to open it in a modal iframe (with angular-material) setting up the src with the xml data, both tests opens the XML with the URL but with this error:

Error loading the stylesheet: unknown error (805303f4)

I have test it in firefox and chrome, and chrome is more clear with the error details in the console:

Unsafe attempt to load URL route-to-xsl.xsl from frame with URL data:text/xml,... Domains, protocols and ports must match.

I tried to use an absolute path to the stylesheet including same protocols, domains etc. but the error remains (both in iframe or new tab options).

Any help pls, thanks.

解决方案

As this is client-side Javascript inside of web browsers like Firefox or Chrome I would suggest to use XSLTProcessor to perform the XSLT transformation, you can pull in the XSLT stylesheet using XMLHttpRequest, parse your responseData using DOMParser and then you can use XSLTProcessor for the transformation.

I don't think you will get the browser to execute an XSLT referenced in the data URL, unless the XSLT is embedded itself as data:

var encodedXslt = 'data:application/xml,' + encodeURIComponent(document.getElementById('xslt').textContent);


var xmlCode = document.getElementById('xml').textContent;
var pi = '<?xml-stylesheet type="text/xsl" href="' + encodedXslt + '"?>';

var encodedXml = 'data:application/xml,' + encodeURIComponent(pi + xmlCode);

window.frames.xmlDisplay.location.href = encodedXml;

<script id="xslt" type="application/xml+xslt">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="list">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>
  <xsl:template match="item">
    <li>
      <xsl:apply-templates/>
    </li>
  </xsl:template>
</xsl:stylesheet>
</script>

<script id="xml" type="application/xml">
  <list>
    <item>foo</item>
    <item>bar</item>
  </list>
</script>

<iframe name="xmlDisplay" width="100%" height="300"></iframe>

However, it seems only Mozilla swallows that attempt and applies the XSLT, Chrome continues to complain about an unsafe attempt. So I think it is better and more portable to implement any XSLT transformation where you have the input as a Javascript string with XSLTProcessor.

这篇关于从data:text/xml调用XSLT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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