如何使用JavaScript / HTML文件运行XSL文件 [英] how to run XSL file using JavaScript / HTML file

查看:65
本文介绍了如何使用JavaScript / HTML文件运行XSL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript函数运行xsl文件。我写了一个javascrpt函数,它在Firefox和Crom上运行良好,但它无法在Internet Explorer上运行

i want to run xsl file using javascript function. I wrote a javascrpt function which is working well with Firefox and Crom but it is not working on Internet Explorer

     function loadXMLDoc(dname)
     {
        if (window.XMLHttpRequest)
          {
              xhttp=new XMLHttpRequest();
          }
        else
          {
              xhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xhttp.open("GET",dname,false);
        xhttp.send("");
        return xhttp.responseXML;
    }

    function displayResult()
    {
        xml=loadXMLDoc("NewXml.xml");
        xsl=loadXMLDoc("NewFile.xsl");
        // code for IE
        if (window.ActiveXObject)
         {
             ex=xml.transformNode(xsl);
             document.getElementById("example").innerHTML=ex;
         }

        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument)
          {
              xsltProcessor=new XSLTProcessor();
              xsltProcessor.importStylesheet(xsl);
              resultDocument = xsltProcessor.transformToFragment(xml,document);
              document.getElementById("example").appendChild(resultDocument);
          }
    }

请通过修改此代码或其他代码来帮助我所以我可以使用Internet Explorer。

Please help my by modifying this code or by another code so that i can work with Internet Explorer.

谢谢

推荐答案

为了应用xsl trasformation,我使用并推广了 Sarissa 。它是一个包含不同浏览器的xml apis的crossbrowser库。

To apply xsl trasformation I use, and promote, Sarissa. It is a crossbrowser library that envelope the xml apis of the different browser.

有关Sarissa转换的示例,您可以直接前往 howto page
但它与此类似:

For an example of a trasformation with Sarissa you can head straight to the howto page but it is similar to this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <title>sarissa test1</title>
  <script type="text/javascript" src="sarissa.js"></script>
</head>
<body>
  <script type="text/javascript">
     function loadXMLDoc(dname) {
       xhttp = new XMLHttpRequest();
       xhttp.open("GET",dname,false);
       xhttp.send("");
      return xhttp.responseXML;
     }

     var processor = new XSLTProcessor();
     var theXML = loadXMLDoc('xml.xml');
     var theXSL = loadXMLDoc('xsl.xsl');

     // prepare the processor
     processor.importStylesheet(theXSL);

     var theResult = processor.transformToDocument(theXML);
     // now you have a DomDocument with the result
     // if you want to serialize (transform to a string) it you van use

     document.write(new XMLSerializer().serializeToString(theResult));  
  </script>
</body>
</html>

快速,简单,保持并且您可以专注于创建xsl而不是努力克服浏览器差异。

Quick, easy, mantained and you can focus your efforts to the xsl creation instead than struggling to overcome the browsers differences.

单个故障,对我来说是一个加号,但有人可能会将其视为限制,代码是在GPL 2.1或更高版本下发布的,或者,如果您愿意Apache License 2.0或更高版本

The single glitch, to me is a plus but someone may see it as a limitation, the code is released under GPL 2.1 or higher or, if you prefer Apache Licence 2.0 or higher

编辑:我的错误,我发布了一个旧的(和错误的代码)而没有检查它。创建了一个新版本,在firefox,chrome,ie8,ie7上模仿你的脚本(并测试它),它运行完美无缺。我使用了谷歌搜索中找到的前两个xsl / xml(我在下面报告它们的
完整性)。按原样和xsl / xml一起尝试代码。如果出现问题也报告故障(我们可以通过比更深入的错误描述更有效 - 不工作 - )。当代码行为异常时会发生什么? (浏览器冻结,报告错误,返回空白结果)

Mine fault, I posted an old (and wrong code) without checking it. Created a new version miming your script (and tested it) on firefox, chrome, ie8, ie7 and it worked flawlessy. I used the first two xsl/xml found on a google search (I report them below for completeness). Try the code as is and with your xsl/xml. If it goes wrong report the malfunction too (we can be more effective with a deeper error description than - don't work - ). What happens when the code misbehave? (the browser freeze, report an error, return a blank result)

xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--?xml-stylesheet type="text/xsl" href="xsl.xsl"?-->
<tutorials>
  <tutorial>
    <name>XML Tutorial</name>
    <url>http://www.quackit.com/xml/tutorial</url>
  </tutorial>
  <tutorial>
    <name>HTML Tutorial</name>
    <url>http://www.quackit.com/html/tutorial</url>
  </tutorial>
</tutorials>

xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- try to enable/disable the xsl:output -->
<!-- xsl:output method="xml" 
                version="1.0" 
                encoding="iso-8859-1" 
                indent="yes"/-->

<xsl:template match="/">
<html>
<head>
<title>XML XSL Example</title>
<style type="text/css">
body
{
margin:10px;
background-color:#ccff00;
font-family:verdana,helvetica,sans-serif;
}

.tutorial-name
{
display:block;
font-weight:bold;
}

.tutorial-url
{
display:block;
color:#636363;
font-size:small;
font-style:italic;
}
</style>
</head>
<body>
<h2>Cool Tutorials</h2>
<p>Hey, check out these tutorials!</p>
  <xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="tutorial">
  <span class="tutorial-name"><xsl:value-of select="name"/></span>
  <span class="tutorial-url"><xsl:value-of select="url"/></span>
</xsl:template>

</xsl:stylesheet>

这篇关于如何使用JavaScript / HTML文件运行XSL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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