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
精彩评论