469,573 Members | 1,775 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

document.evaluate and @innerHTML

In Firefox 1.5 (this question is Mozilla specific as I am using
greasemonkey) I would like to be able to use document.evaluate to
return the first TD entry that shows ^\s*MySearchText\s*$. As I
understand it, xpath doesn't yet have regular expressions so I thought
to do:

function findNode (srch) {
var node;
var expr="//td[contains(@innerHTML,'"+srch+"')]";
var RE = new RegExp("\\s*" + srch + "\\s*$");
var xpathResult = document.evaluate(expr, document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < xpathResult.snapshotLength; i++) {
node = xpathResult.snapshotItem(i);
if (node.innerHTML.match(RE)) return (node); // node found
}

return null; // node was not found
}
This always returns null though. Specifically,
xpathResult.snapshotLength is always 0 since document.evaluate
evidently doesn't like that @innerHTML. Short of looping through all
TDs, is there another approach someone might suggest?

Thanks,
Csaba Gabor from Vienna

Dec 14 '05 #1
5 6320


Csaba Gabor wrote:

var expr="//td[contains(@innerHTML,'"+srch+"')]";


@name in XPath accesses an attribute of an element, innerHTML is not an
attribute of an element, it is only a property exposed in the browser DOM.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 14 '05 #2


Csaba Gabor wrote:
In Firefox 1.5 (this question is Mozilla specific as I am using
greasemonkey) I would like to be able to use document.evaluate to
return the first TD entry that shows ^\s*MySearchText\s*$. var xpathResult = document.evaluate(expr, document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);


Note also that you could or even should use
XPathResult.FIRST_ORDERED_NODE_TYPE if you are only looking for the
first node e.g.
var singleResultNode =
document.evaluate(xPathExpression, document, null, 9,
null).singleNodeValue;
if (singleResultNode != null) {
// use node here
}
That way the XPath implementation only needs to find the first node and
does not need to build the complete resulting node set.

Other optimizations make sense, for instance if you want to look for td
elements then those usually sit only inside of the body so you could
write an expression .//td and evaluate it with document.body being the
context node.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 14 '05 #3
Martin Honnen wrote:
Csaba Gabor wrote:
In Firefox 1.5 (this question is Mozilla specific as I am using
greasemonkey) I would like to be able to use document.evaluate to
return the first TD entry that shows ^\s*MySearchText\s*$.
Note also that you could or even should use
XPathResult.FIRST_ORDERED_NODE_TYPE if you are only looking for the
first node e.g.
var singleResultNode =
document.evaluate(xPathExpression, document, null, 9,
null).singleNodeValue;
if (singleResultNode != null) {
// use node here
}
That way the XPath implementation only needs to find the first node and
does not need to build the complete resulting node set.


Thank you Martin, your comments were very helpful. So it seems that I
really do have to stumble through all the elements (ie. I'm not really
saving anything over document.body.getElementsByTagName('TD') it seems
to me). Given that document.evaluate is supposed to be used for
trawling through the DOM, it's unfortunate that the designers did not
include the ability to search on properties as they are some of the
most important characteristics (such as text / positioning) to search
on.
Other optimizations make sense, for instance if you want to look for td
elements then those usually sit only inside of the body so you could
write an expression .//td and evaluate it with document.body being the
context node.


You show a period in front of that //td
(How) does that alter the meaning? That is, how does
document.evaluate("//td", document.body, null, 9, null)
differ from
document.evaluate(".//td", document.body, null, 9, null)

The greasemonkey documentation has a few examples at:
http://diveintogreasemonkey.org/patt...attribute.html
but they are very primitive. On the other hand, I didn't see any //
examples in the documentation that they pointed me to. Would you know
of a good source for examples?

Thanks again,
Csaba

Dec 14 '05 #4


Csaba Gabor wrote:
You show a period in front of that //td
(How) does that alter the meaning? That is, how does
document.evaluate("//td", document.body, null, 9, null)
differ from
document.evaluate(".//td", document.body, null, 9, null)


The first (//td) is an absolute XPath expression, it always starts from
the root node (document node in the DOM) while the second (.//td) is a
relative XPath expression.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 14 '05 #5
Csaba Gabor wrote:
In Firefox 1.5 (this question is Mozilla specific as I am using
greasemonkey) I would like to be able to use document.evaluate to
return the first TD entry that shows ^\s*MySearchText\s*$. As I
understand it, xpath doesn't yet have regular expressions so I thought

....

the function you are lookin for is called string() - see here
http://www.w3.org/TR/xpath#section-String-Functions
- it returns context node converted into string (but it doesn't handle
RegExps) - if you wanna find first ocurrance of string "srch" in all td
nodes - the evaluator should loook like this:

function findNode (srch) {
return document.evaluate(
"//td[contains(string(),'"+srch+"')]",
document,
null,
9,
null).singleNodeValue;
}

as i mentioned above it wouldn't catch regexp - to handle it you have
to use standard DOM methods with loop like this:

function findNodeRE (srch) {
var allTd = document.getElementsByTagName('TD');
for(var i=0;i<allTd.length;i++){
if(srch.test(allTd[i].textContent))
return allTd[i];
}
}

pay attention that "srch" variable in second function is RegExp object

Dec 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by james.kingston | last post: by
2 posts views Thread by Björn Langhof | last post: by
1 post views Thread by shellon | last post: by
1 post views Thread by shellon | last post: by
3 posts views Thread by SJ | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.