简体   繁体   English

当XSL使用xsl:include时,如何在Javascript中应用XSL?

[英]How do I apply XSL with Javascript when the XSL uses xsl:include?

I'm working with the XSLT code sample on W3Schools: https://www.w3schools.com/xml/xsl_client.asp 我正在W3Schools上使用XSLT代码示例: https : //www.w3schools.com/xml/xsl_client.asp

I have this section of code working in IE with basic xslt processing... 我有这段代码在IE中使用基本xslt处理...

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(filename)
{
if (window.ActiveXObject)
  {
  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
else
  {
  xhttp = new XMLHttpRequest();
  }
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}

function displayResult()
{
xml = loadXMLDoc("cdcatalog.xml");
xsl = loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
  {
  ex = xml.transformNode(xsl);
  document.getElementById("example").innerHTML = ex;
  }
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html> 

The problem is the XSL I'm scaling this solution for has a complex structure, so there are several xsl:include statements to pull in other xsl templates ( https://www.w3schools.com/xml/ref_xsl_el_include.asp ). 问题是我正在扩展此解决方案的XSL具有复杂的结构,因此有多个xsl:include语句可以引入其他xsl模板( https://www.w3schools.com/xml/ref_xsl_el_include.asp )。 Javascript isn't processing the xsl:include imports and the XSLT processor keeps complaining about missing templates. Javascript未处理xsl:include导入,并且XSLT处理器不断抱怨缺少模板。

How do I get Javascript to process xsl:include? 如何获取Javascript处理xsl:include?

Using XSLT in IE and Edge through Javascript is a bit of a struggle as initially you would do that by using the various MSXML (Microsoft's COM based XML, XPath and XSLT 1.0 software package) components directly with the Microsoft specific new ActiveXObject . 在IE和Edge中通过Javascript使用XSLT有点困难,因为最初您可以通过将各种MSXML(基于Microsoft COM的XML,XPath和XSLT 1.0软件包)组件直接与Microsoft特定的new ActiveXObject However, to be more compliant with other browsers they have, at least on the surface, moved to support XMLHttpRequest (in IE and Edge) and XSLTProcessor (in Edge), but as far as I understand under the hood it is still MSXML 6 that is used, in particular for XSLT. 但是,为了与其他浏览器更加兼容,至少在表面上,它们已迁移为支持XMLHttpRequest (在IE和Edge中)和XSLTProcessor (在Edge中),但是据我所知,仍然是MSXML 6用于XSLT,尤其是XSLT。

As MSXML 6 by default sets the property resolveExternals to false the use of xsl:import and xsl:include fails, so to use MSXML 6 programmatically (in the browser you do that with Javascript) you first need to set that property to true to have MSXML then resolve and load external components like imported or included stylesheet modules. 由于MSXML 6默认情况下将属性resolveExternals为false,因此xsl:importxsl:include失败,因此要以编程方式使用MSXML 6(在使用Javascript的浏览器中进行操作),首先需要将该属性设置为true才能具有然后,MSXML解析并加载外部组件,例如导入或包含的样式表模块。 Only these properties are not well exposed when you load XML through XMLHttpRequest. 通过XMLHttpRequest加载XML时,只有这些属性无法很好地公开。

I have unsuccessfully tried to do that without resorting to too much IE specific code with the use of new ActiveXObject in https://martin-honnen.github.io/xslt/2018/test2018112004.html (code visible at https://github.com/martin-honnen/martin-honnen.github.io/blob/master/xslt/2018/test2018112004.html ), however, when I abandon using XMLHttpRequest for IE (respectively where XSLTProcessor is not supported) and directly use all MSXML 6 components with new ActiveXObject and set the above named property, I get IE 11 on Windows 10 to resolve and use an imported stylesheet through Javascript in https://martin-honnen.github.io/xslt/2018/test2018112005.html (code visible at https://github.com/martin-honnen/martin-honnen.github.io/blob/master/xslt/2018/test2018112005.html ). 我尝试通过在https://martin-honnen.github.io/xslt/2018/test2018112004.html (在https:// github上可见的代码)中使用new ActiveXObject而不使用过多的IE特定代码来尝试执行此操作.com / martin-honnen / martin-honnen.github.io / blob / master / xslt / 2018 / test2018112004.html ),但是,当我放弃对IE使用XMLHttpRequest(分别不支持XSLTProcessor的地方)而直接使用所有MSXML时6个具有new ActiveXObject组件并设置了上述命名属性,我在Windows 10上获得IE 11,以通过https://martin-honnen.github.io/xslt/2018/test2018112005.html中的 Javascript解析和使用导入的样式表在https://github.com/martin-honnen/martin-honnen.github.io/blob/master/xslt/2018/test2018112005.html上可见)。 I haven't tried with earlier IE versions, other than trying to use the emulation mode in the IE 11 developer tools, based on that it also works with IE 10 and 9. 除了尝试在IE 11开发人员工具中使用仿真模式之外,我还没有尝试使用较早的IE版本,因为它也可以与IE 10和9一起使用。

So what I do in 所以我在做什么

  function loadDocForXslt(url) {
       return new Promise(function(resolve) {
         if (typeof XSLTProcessor === 'undefined') {
           var doc = new ActiveXObject('Msxml2.DOMDocument.6.0');
           doc.setProperty('ResolveExternals', true);
           doc.onreadystatechange = function() {
               if (doc.readyState === 4) {
                   resolve(doc);
               }
           };
           doc.load(url);
         }
         else {
             var req = new XMLHttpRequest();
             req.open("GET", url);
             if (typeof XSLTProcessor === 'undefined') {
               try {
                 req.responseType = 'msxml-document';
               }
               catch (e) {}
             }
             req.onload = function() {
               resolve(this.responseXML)
             }
             req.send();
         }
       });
  }

is creating an MSXML 6 XML DOM document with new ActiveXObject('Msxml2.DOMDocument.6.0') directly and set is property doc.setProperty('ResolveExternals', true); 正在直接使用new ActiveXObject('Msxml2.DOMDocument.6.0')创建一个MSXML 6 XML DOM文档,并将其设置为属性doc.setProperty('ResolveExternals', true); so enable xsl:import/xsl:include . 因此启用xsl:import/xsl:include

Of course, as with most stuff you do in the browser, the same origin policy by default doesn't allow you to load XML from anything but the same origin your HTML with the script comes from. 当然,就像您在浏览器中所做的大多数事情一样,默认情况下,相同的原始策略不允许您从任何内容加载XML,但与脚本产生的HTML相同的原始来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM