开发者

XPath expression exception

I am not really sure if i am doing this right with XPath expression. I am trying to locate a text on DOM and this text is assingned to a variable. I have text stored in SQLite and i have retrievd the text and i am trying to locate it on the webpage which actually contains the text. so i ahve the following code:

var searchText = dataset[x]['selectedText'];
        alert(dataset[x]['selectedText']);
        var res = googbar_frames[0].contentDocument.evaluate("//*[.=searchText]",googbar_frames[0].conte开发者_JAVA技巧ntDocument.body,null,XPathResult.ANY_TYPE,null);
        alert(res.snapshotLength);

And i get the following error.

Error: Permission denied for <http://en.wikipedia.org> to call method XPathException.toString on <>.
Error: Permission denied for <http://en.wikipedia.org> to call method XPathException.toString on <>.

Have got the expression correct. I am trying to look for the text on DOM. Or am i going wrong somwehere? cheers


[This is an answer to a followup question to my original answer. Sorry, stackoverflow purists! The comments thingie doesn't always work.]

Yes, this is what I talked about earlier. ".//text()[contains(.,searchText)]" doesn't use the JavaScript variable searchText, it's just a string.

You could construct the XPath expression using searchText. There's no variable interpolation in JS, so you have to construct the XPath expression using manual concatenation, like this:

var xpathExpr = ".//text()[contains(.,'" + searchText + "')]";

...except this fails if searchText includes a single quote ('), so you have to escape it and possibly other characters with special meaning in this context in XPath. I'm not up to figuring out for you what exactly and how you need to escape in searchText, it would involve either searching the internets for an existing solution or reading the xpath spec to learn the grammar in this case.

So I stick with my original reply:

what are you actually trying to do? Perhaps using the interfaces the Firefox Find toolbar uses would be a better idea?


[This is the original answer to the question, see also a followup question and an answer to the follow-up]

The expression is incorrect. Running this (i.e. using a correct expression) in the browser.xul context:

 content.document.evaluate("//*[.=searchText]",
   content.document.body,null,XPathResult.ANY_TYPE,null); 

- while not very useful, produces no such exception.

Do you actually include searchText in the XPath expression? Is it quoted properly? I expect your XPath results will not be what you expect it to be, so what are you actually trying to do? Perhaps using the interfaces the Firefox Find toolbar uses would be a better idea?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜