开发者

Get nodes from responseText using xpath

I want to get all the div elements from a website, using xpath. I'm using Opera 11.50. Cross site xhr works because this is an extension (and responseText gets the right content, and I can say getElementsByTagName('div'), but I need to use xpath). Please help.

var xhr = new window.XMLHttpRe开发者_JAVA技巧quest();
xhr.open('GET','http://www.msn.com',true);
xhr.onreadystatechange = function(){
    if(this.readyState == 4 && this.status == 200){

        //first try
        var root = this.responseXML;
        var nodes = root.selectNodes('//div'); //nodes.length = 0 !!


        //second
        var doc = xhr.responseXML;
        var divs = doc.evaluate( '//div', doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );
        // divs.snapshotLength = 0 ...    


    }
}


Well then it is a namespace issue. I guess doing

var nodes = this.responseXML.selectNodes('//xhtml:div', function (prefix) { if (prefix === 'xhtml') return 'http://www.w3.org/1999/xhtml'; else return null; }) 

should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜