2011-04-04 33 views
2

我是学习XSLT的新手,并且遇到了一些我不太了解的东西。我需要在转换文档之前添加一个XSLT参数。我可以对非IE浏览器做到这一点,像这样:如何使用XSLT转换XML文档并在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版本的代码中,我不能简单地在转换之前添加参数。我大量搜索并看到不同的东西,告诉我创建各种不同MSXML版本的新ActiveX对象,而且我对整个事件深感困惑。

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

除了IE浏览器,如果可能,可以有人解释IE如何与XSLT的不同,以FF/O/C的整个概念/其他文明交易浏览器?

回答

0

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"; 
} 
+0

我确实找到了该页面并将其剔除,但它显然需要另一个外观。我想我会明天再尝试一次,并用更多的咖啡。在这一点上,我最常被我在不同的代码片段中找到的所有不同的MSXML事物所困惑,哪些是合适的。尽管我感谢你的帮助! – yphmsm 2011-04-04 05:51:18

+0

我认为它是XSLTProcessor.setParameter(“Parameter Name”,“Parameter Value”)而不是XSLTProcessor.addParameter(“Parameter Name”,“Parameter Value”) – Gilbou 2011-06-14 10:51:04

1

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