【问题标题】:How do I transform an XML document using XSLT and add a parameter in IE?如何使用 XSLT 转换 XML 文档并在 IE 中添加参数?
【发布时间】:2011-04-04 02:08:59
【问题描述】:

我是学习 XSLT 的新手,遇到了一些我真的不太明白的东西。我需要在转换文档之前添加一个 XSLT 参数。对于非 IE 浏览器,我可以这样做:

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("cdcatalog.xml");
    xsl = loadXMLDoc("cdcatalog.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);
    }
}

现在,我可以在非 IE 浏览器上执行此操作,创建一个新的 XSLT 处理器对象,导入样式表,您只需在转换过程之前添加参数。不过,对于 IE 版本的代码,这似乎都没有发生,我不能简单地在转换之前添加参数。我在 Google 上大肆搜索,看到不同的东西告诉我要创建各种不同 MSXML 版本的新 ActiveX 对象,我对整个事件深感困惑。

使用上面的代码,我该怎么做:
xsltProcessor.setParameter(null,"PARAMNAME","PARAMVALUE");

除了 IE,如果可能的话,谁能解释一下 IE 如何以与 FF/O/C/其他文明浏览器不同的方式处理 XSLT 的整个概念?

【问题讨论】:

    标签: javascript xml xslt


    【解决方案1】:

    您可以尝试使用 Sarissa,它是一个提供跨浏览器 XSLT 转换 API 的抽象层。

    【讨论】:

      【解决方案2】:

      根据this page,可以使用

      XSLTProcessor.addParameter("Parameter Name", "Parameter Value");
      

      XSLTProcessor 的创建位置

      var XSLTCompiled = new ActiveXObject("MSXML2.XSLTemplate");
      XSLTCompiled.stylesheet = XSL.documentElement;
      var XSLTProcessor = XSLTCompiled.createProcessor();
      

      变换调用也不同。

      XSLTProcessor.transform();
      

      无论如何,您的要求似乎有一个非常详尽的解释。

      不久前我进行了跨浏览器 XSLT 转换,这是我使用的代码。 createDocument 只是一个返回 DOM 文档的函数。我没有做样式表参数,所以这可能有点离题,但无论如何它适用于 IE6+ 和 Firefox 1.5+。

      // arguments can be string (uri of document) or document node
      function xslTransform( content, transform, options )
      {
          if ("string" == typeof content)   content = createDocument( content );
          if ("string" == typeof transform) transform = createDocument( transform );
      
          var targetEle;
      
          if (options && options.target) targetEle = document.getElementById(options.target);
      
          if (targetEle && options.replace)
              while (targetEle.hasChildNodes())
                  targetEle.removeChild( targetEle.firstChild );
      
          if (window.XSLTProcessor)
          {
              var processor = new XSLTProcessor();
              processor.importStylesheet( transform );
              var frag = processor.transformToFragment( content, document );
              if (targetEle)
                  targetEle.appendChild( frag );
          }
          else if (window.ActiveXObject)
          {
              if (targetEle)
                  targetEle.innerHTML = content.transformNode( transform );
          }
          else return "XSLT not supported";
      }
      

      【讨论】:

      • 我确实找到了该页面并浏览了它,但它显然值得再看一遍。我想我明天会再试一次,并提供更多的咖啡。在这一点上,我对在不同的代码 sn-ps 中发现的所有不同的 MSXML 内容以及哪一个是合适的感到困惑。不过,我感谢您的帮助!
      • 我认为是 XSLTProcessor.setParameter("Parameter Name", "Parameter Value") 而不是 XSLTProcessor.addParameter("Parameter Name", "Parameter Value")
      猜你喜欢
      • 2020-03-27
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多