简体   繁体   English

Internet Explorer 是否支持 XPATH?

[英]Is XPATH supported in Internet Explorer?

Please check the below code, it's working fine in Firefox but not working in internet explorer.请检查以下代码,它在 Firefox 中工作正常,但在 Internet Explorer 中不工作。

Can any one please help me on this?有人可以帮我吗?

HTML file: (finaltest.html) HTML 文件:(finaltest.html)

<html>
    <head>
        <title>This is testing</title>
        <script type="text/javascript">
            var items= new Array();
            var details = new Array();
            var contents = new Array();

            function allCall()
            {
                readXMLUsingXPATH();
                populateList();
            }
            function populateList()
            {
                for(var list=0; list<items.length; list++){
                    var temp= new Option(items[list],items[list]);
                    document.getElementById('sel').options.add(temp);
                    //alert("in populatelist");
                }
            }

            function addContent(divName, content) 
            {
                //alert(content);
                document.getElementById(divName).innerHTML = content;
            }

            function loadXML(){
                var xmlhttp;
                if(window.XMLHttpRequest){
                    xmlhttp = new XMLHttpRequest();
                }
                else{
                    xmlhttp = new ActiveXObject("Micrsoft.XMLHTTP");
                }
                xmlhttp.open("GET","details.xml",false);
                xmlhttp.send();
                return xmlhttp.responseXML;
            }

            function readXMLUsingXPATH(){
                var xmlDoc = loadXML();
                if(xmlDoc == null){
                    alert("XML HTTP object is null");
                    return;
                }
                var path1 = "/Root/Item/name";
                var path2 = "/Root/Item/details";
                var path3 =  "/Root/Item/content";
                var xmlNoadList1,xmlNoadList2,xmlNoadList3,result1,result2,result3,name,detail,content,list;
                if(window.ActiveXObject){
                    xmlNoadList1 = xmlDoc.selectNodes(path1);
                    xmlNoadList2 = xmlDoc.selectNodes(path2);
                    xmlNoadList3 = xmlDoc.selectNodes(path3);
                    for(list=0; list<xmlNoadList1.length; list++){
                        items[list]=xmlNoadList1[list].childNodes[0].nodeValue;
                        name = xmlNoadList1[list].childNodes[0].nodeValue;
                    }
                    for(list=0; list<xmlNoadList2.length; list++){
                        details[list]=xmlNoadList2[list].childNodes[0].nodeValue;
                        detail = xmlNoadList2[list].childNodes[0].nodeValue;
                    }
                    for(list=0; list<xmlNoadList3.length; list++){
                        contents[list]=xmlNoadList3[list].childNodes[0].nodeValue;
                        content = xmlNoadList3[list].childNodes[0].nodeValue;
                    }

                }
                else if(document.implementation && document.implementation.createDocument){
                    xmlNoadList1 = xmlDoc.evaluate(path1, xmlDoc, null, XPathResult.ANY_TYPE, null);
                    xmlNoadList2 = xmlDoc.evaluate(path2, xmlDoc, null, XPathResult.ANY_TYPE, null);
                    xmlNoadList3 = xmlDoc.evaluate(path3, xmlDoc, null, XPathResult.ANY_TYPE, null);
                    result1 = xmlNoadList1.iterateNext();
                    result2 = xmlNoadList2.iterateNext();
                    result3 = xmlNoadList3.iterateNext();
                    list=0;
                    while(result1){
                        name = result1.childNodes[0].nodeValue;
                        items[list]=result1.childNodes[0].nodeValue;
                        result1 = xmlNoadList1.iterateNext();
                        list++;
                    }
                    list=0;
                    while(result2){
                        detail = result2.childNodes[0].nodeValue;
                        details[list]=result2.childNodes[0].nodeValue;
                        result2 = xmlNoadList2.iterateNext();
                        list++;
                    }
                    list=0;
                    while(result3){
                        content = result3.childNodes[0].nodeValue;
                        contents[list]=result3.childNodes[0].nodeValue;
                        result3 = xmlNoadList3.iterateNext();
                        list++;
                    }
                }
            //alert("in xml");
            }
            function itemDetails(name,ind){
                //alert(details[ind]);
                document.getElementById(name).innerHTML = details[ind] + "</br>" + contents[ind];
            }

        </script>
    </head>
    <body >

        <form name="myForm">
            Content to be added:
            <SELECT name="sel" id="sel" size="4" onChange="addContent('result', this[this.selectedIndex].text);">
            </SELECT>
            <input type="button" value="Details" onClick="itemDetails('result', document.getElementById('sel').selectedIndex)">
        </form>
        <br><br>
        Your content will be added dynamically below:
        <div id="result"></div>
        <script type="text/javascript">
        onload=allCall;
        </script>
    </body>
</html>

XML file: (details.xml) XML 文件:(details.xml)

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Item>
        <name>Item1</name>
        <details>Item1 Details</details>
        <content>Item1 Content</content>
    </Item>
    <Item>
        <name>Item2</name>
        <details>Item2 Details</details>
        <content>Item2 Content</content>
    </Item>
    <Item>
        <name>Item3</name>
        <details>Item3 Details</details>
        <content>Item3 Content</content>
    </Item>
    <Item>
        <name>Item4</name>
        <details>Item4 Details</details>
        <content>Item4 Content</content>
    </Item>
    <Item>
        <name>Item5</name>
        <details>Item5 Details</details>
        <content>Item5 Content</content>
    </Item>
    <Item>
        <name>Item6</name>
        <details>Item6 Details</details>
        <content>Item6 Content</content>
    </Item>
    <Item>
        <name>Item7</name>
        <details>Item7 Details</details>
        <content>Item7 Content</content>
    </Item>
    <Item>
        <name>Item8</name>
        <details>Item8 Details</details>
        <content>Item8 Content</content>
    </Item>
</Root>

You haven't said what exactly happens when you run your code sample with IE, which version of IE you tried, which error you get for which statement.您还没有说当您使用 IE 运行代码示例时究竟会发生什么,您尝试了哪个版本的 IE,您针对哪个语句遇到了哪个错误。 One possible issue is simply a typo in your code, instead of xmlhttp = new ActiveXObject("Micrsoft.XMLHTTP") you need xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") .一个可能的问题只是代码中的拼写错误,而不是xmlhttp = new ActiveXObject("Micrsoft.XMLHTTP")您需要xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") Whether that is the part of your code causing your problem is hard to tell, it depends on the IE version and its settings whether that code branch is used at all.这是否是导致问题的代码部分很难说,这取决于 IE 版本及其设置是否使用了该代码分支。

According to a blog post at根据博客文章

Internet Explorer (at least up to IE8) doesnt implement the DOM Level 3 XPath Specs on document but provides it's own implementation via the MSXML2.DOMDocument ActiveX object on XML documents. Internet Explorer(至少到 IE8)没有实现document上的DOM 级别 3 XPath 规范,但通过 Z3501BB093D363810B6CFEDFZ8 文档上的MSXML2.DOMDocument ActiveX object 提供了自己的实现。

A pitfall in IE 9 is that it supports some methods that previously have been associated with other browsers. IE 9 中的一个缺陷是它支持一些以前与其他浏览器相关联的方法。 For example I have a code that was working in IE 8 and Firefox, but not in IE 9. The explanation was very simple: IE 9 was attempting to run down the Firefox branch, and failing of course: IE9 does not support document.evaluate method of Xpath.例如,我有一个在 IE 8 和 Firefox 中工作的代码,但在 IE 9 中没有。解释很简单:IE 9 试图运行 Firefox 分支,当然失败了:IE9 不支持 document.evaluate Xpath的方法。

    // Code for Firefox
    // Wrong!
    if ( document.implementation && document.implementation.createDocument ) {  
    // stops IE8, but not IE9 from running the below code
    // do some Xpath stuff ie. document.evaluate(//more code here )}

    // Correct
    if ( navigator.appName != "Microsoft Internet Explorer" ) {  

    // stops IE 8 and IE 9 from running the below code
    // do some Xpath stuff  ie. document.evaluate(//more code here )   }

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

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