views:

318

answers:

3

Hi 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].contentDocument.body,null,XPathResult.ANY_TYPE,null);
     alert(res.snapshotLength);

And i get the following error.

Error: Permission denied for <http://en.wikipedia.org&gt; to call method XPathException.toString on <>.
Error: Permission denied for <http://en.wikipedia.org&gt; 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

A: 

[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?

Nickolay
hey ,what do you mean by include searchText in the XPath expression?var searchText = dataset[x]['selectedText']; // that code their retrieves a text e.g football from the database. so the text to be searched is 'football'.and as for the contentDocument thing, i ahve got frames in the main browser and its intended for firefox.
fftoolbar
Thank you very much. Yes i did manage to get what i wanted. and that answer was of great help.
fftoolbar
+1  A: 

Hi Nick, I figured it somewhat but now i think my XPath expression is doing something wrong. Can u please help me with this. I have a variable var searchText = 'I am the one' and i do the following

var res = document.evaluate(".//text()[contains(.,searchText)]",document.body,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);

and

alert(res.snapshotLength);

is zero it has to be one. But when i actually pass the string like below:

var res = document.evaluate(".//text()[contains(.,'I am the one')]",document.body,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
alert(res.snapshotLength);

gives me one which is correct? So i have a feeling that they way i am passing the variable may be wrong? Any suggestions.. i tried

".//text()[contains(.,$searchText)]"

its with the XPath expression. cheers.

fftoolbar
I wouldn't be surprised if it's because you're using evaluate. Doing seomthing like ".//text()[contains(.,'"+searchText+"')]"instead would work. Think evaluate as another session which cannot access variables from the previous one.
lithorus
+1  A: 

[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?

Nickolay