XML跨浏览器支持

时间:2009-12-27 08:00:36

标签: xml xslt cross-browser

我需要帮助让文件在Firefox中运行:我尝试调整脚本以便我的文件在IE和Firefox中运行,但到目前为止它仍然只能在IE中运行。 (该文件可在http://www.eyle.org/crosstest.html进行测试 - 只需在文本框中使用IE键入单词Mike(在Firefox中不起作用).HTML文档为:

<!DOCTYPE html PUBLIC 
     "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
        var xmlDoc;
        //loads xml using either IE or firefox
        function loadXmlDoc()
        {
            //test for IE
            if(window.ActiveXObject)
            {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load("books2.xml");
            }

            //test for Firefox
            else if(document.implementation && document.implementation.createDocument)
            {
                xmlDoc = document.implementation.createDocument("","",null);
                xmlDoc.load("books2.xml");
            }
            //if neither
            else
            {
                document.write("xml file did not load");
            }
        }

        //window.onload = loadXmlDoc();
        var subject;
        //getDetails adds value of txtField to var subject in outputgroup(subject)
        function getDetails()
        {
            //either this or window.onload = loadXmlDoc is needed
            loadXmlDoc();
            var subject = document.getElementById("txtField1").value;
            function outputgroup(subject) 
            {
                var xslt = new ActiveXObject("Msxml2.XSLTemplate");
                var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
                var xslProc;
                xslDoc.async = false;
                xslDoc.resolveExternals = false;
                xslDoc.load("contains3books.xsl");
                xslt.stylesheet = xslDoc;
                xslProc = xslt.createProcessor();
                xslProc.input = xmlDoc;
                xslProc.addParameter("subj", subject);
                xslProc.transform();
                document.write(xslProc.output);
            }
            outputgroup(subject);
        }

    </script>
</head>

<body>
    <input type="text" id="txtField1">
    <input type="submit" onClick="getDetails(); return false">
</body>
</html>

该文件包含books2.xml和contains3books.xsl(我已将这些文件的代码放在... ww.eyle.org/books2.xml ... ww.eyle.org/contains3books.xsl(NB) :用http:// www)替换... ww。

2 个答案:

答案 0 :(得分:0)

ActiveX是帮助IE在浏览器中加载其他应用程序/控件的机制。它仅适用于IE,没有其他浏览器支持它。

访问以下网站了解更多信息:

http://www.reloco.com.ar/mozilla/compat.html

http://support.mozilla.com/en-US/kb/ActiveX

答案 1 :(得分:0)

您的outputgroup函数使用ActiveXObject创建来创建xslt,这是IE特定的

对于mozilla,请使用XSLTProcessor();

例如对于firefox

var processor = new XSLTProcessor(); //create object 

xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.load("books2.xml");

xslDoc.load("contains3books.xsl"); 
xslt.stylesheet = xslDoc;
processor.importStylesheet(xslDoc);//import a stylesheet 

var output = processor.transformToFragment(xmlDoc);