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 5 6757
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/
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/
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
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: james.kingston |
last post by:
In my first experimentation with js, I'm writing a greasemonkey script
which adds links to a page which, when clicked, will replace their
parent element with the contents of an element from another...
|
by: ppcguy |
last post by:
i've got this in firefox and it works
var xpath_result = document.evaluate("id('main')/tbody", document, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null );
var table =...
|
by: Björn Langhof |
last post by:
Hello.
I want to evaluate a XPath-Expression only on a subtree of the whole
xml-document.
1. I select a node of the XML-document
2. Then a want to select specific nodes below the node chosen...
|
by: shellon |
last post by:
Hi all:
I met a problem when using document.evaluate() to get text content
using XPath,
my code is as follows:
nodes = document.evaluate("/html/body/div/ul/li", document,
null,XPathResult....
|
by: shellon |
last post by:
Hi all:
when I use XPather(a firefox extension) to evaluate the expression:
"/html/body/table/tbody/tr/td/table/tbody/tr/td/div/ul/li"
it tells me there are 7 matching Nodes.
but when I use...
| |
by: Rob |
last post by:
Hello,
I'm sure this has come up before. I have need for a collection of all
elements/objects in an HTML document that have any kind of an
attribute (HTML or CSS) that is making use of a URL to...
|
by: SJ |
last post by:
Hi,
So I am trying to save off my source HTML to an xml document in order
to cache a large view within my ajax app.
For those of you familiar with this, the innerHTML does not preserve
the...
|
by: jsuser |
last post by:
This is to discuss pros and cons of each other.
|
by: phub11 |
last post by:
Hi all,
I have started using AJAX to populate drop downs from a mySQL database; however, I want the option of modifying the selected OPTION of the drop down using an array of radiobuttons...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |