document.evaluate, 2nd parameter, I don't get it

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

document.evaluate, 2nd parameter, I don't get it

Grumpf@gmail.com
Hello.
I'm trying to develop a little GreaseMonkey script, on the http://forum.hardware.fr
page.

I'd like to get all the links inside all the subsections of the page:
http://www.hardware.fr
The subsections are named 'cat1', 'cat2', etc, up to 'cat16'.

When I do:
  var allLinks, thisLink;
  allLinks = document.evaluate('//a[@href]',
document.getElementById('cat16'), null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (var i = 0; i < allLinks.snapshotLength; i++)
  {
      thisLink = allLinks.snapshotItem(i);
      GM_log(thisLink);
  }

It returns all the links of the page, not the links of the 'cat16'
element. Why ?

Ie, it has the same results than
  allLinks = document.evaluate('//a[@href]', document, null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

According to this page:
http://diveintogreasemonkey.org/patterns/match-attribute.html
it shouldn't:
Quote:
"The second parameter can be any element, and the XPath query will
only return nodes that are children of that element. So if you already
have a reference to an element (say, from document.getElementById or a
member of a document.getElementsByTagName array), you can restrict the
query to search only children of that element."

Thanks a lot.

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: document.evaluate, 2nd parameter, I don't get it

Nickolay Ponomarev
On 8/17/07, [hidden email] <[hidden email]> wrote:
> I'd like to get all the links inside all the subsections of the page:
> http://www.hardware.fr
> The subsections are named 'cat1', 'cat2', etc, up to 'cat16'.
> [...]
>   allLinks = document.evaluate('//a[@href]',
> document.getElementById('cat16'), null,
[...]
> returns all the links of the page, not the links of the 'cat16'
> element. Why ?
>
> Ie, it has the same results than
>   allLinks = document.evaluate('//a[@href]', document, null,
> XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
>
See http://www.w3.org/TR/xpath#path-abbrev

"//para selects all the para descendants of the document root and thus
selects all para elements in the same document as the context node"

".//para selects the para element descendants of the context node"

The "context node" is what you specify in the second parameter to
document.evaluate. This means you need .//a[@href]

> According to this page:
> http://diveintogreasemonkey.org/patterns/match-attribute.html
> it shouldn't:
> Quote:
> "The second parameter can be any element, and the XPath query will
> only return nodes that are children of that element. So if you already
> have a reference to an element (say, from document.getElementById or a
> member of a document.getElementsByTagName array), you can restrict the
> query to search only children of that element."
>
As far as I can see, this is wrong. Please let Mark know about it.

Nickolay
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: document.evaluate, 2nd parameter, I don't get it

Grumpf@gmail.com
In reply to this post by Grumpf@gmail.com
It works perfectly :)
Thanks a lot Nickolay.

Will warn the document's author this afternoon.

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml