Sebastian Fey | 1 May 2006 01:03
Picon

document.evaluate and xpath function position()

hi there,

i just tried to use position() in document.evaluate.
i failed :)

position() returns the position of the current context node in the 
current context node-set.

I want to determine the position of contextNode (type ELEMENT_NODE).

var xpathResult = document.evaluate(
  "position()",
  contextNode,
  null,
  XPathResult.NUMBER_TYPE,
  null);

xpathResult == 1, no matter which position contextNode has.
It seems like evaluate() does not see the current context node-set, but 
takes contextNode as the current context node-set.
Or do i miss anything?
Martin Honnen | 1 May 2006 13:56
Picon
Favicon

Re: document.evaluate and xpath function position()

Sebastian Fey wrote:

> i just tried to use position() in document.evaluate.
> i failed :)

Not really, you get the result you are supposed to get.

> position() returns the position of the current context node in the 
> current context node-set.

The definition says
   "The position function returns a number equal to the context position 
from the expression evaluation context."

> I want to determine the position of contextNode (type ELEMENT_NODE).
> 
> var xpathResult = document.evaluate(
>  "position()",
>  contextNode,
>  null,
>  XPathResult.NUMBER_TYPE,
>  null);
> 
> xpathResult == 1, no matter which position contextNode has.

If you want to count the number of preceding-sibling or the number of
nodes on the ancestor-or-self axis you can certainly do that e.g.
   document.evaluate(
     'count(preceding-sibling::*) + 1',
     contextNode,
(Continue reading)

Sebastian Fey | 1 May 2006 14:28
Picon

Re: document.evaluate and xpath function position()


hey martin, thx for your reply :)

> 
>> i just tried to use position() in document.evaluate.
>> i failed :)
> 
> Not really, you get the result you are supposed to get.

im a little confused.
in the example i provieded:

 >> I want to determine the position of contextNode (type ELEMENT_NODE).
 >>
 >> var xpathResult = document.evaluate(
 >>  "position()",
 >>  contextNode,
 >>  null,
 >>  XPathResult.NUMBER_TYPE,
 >>  null);
 >>
 >> xpathResult == 1, no matter which position contextNode has.

is contextNode the "context node" and the "expression evaluation 
context". this would explain why a element in a set of 1 element is at 
postition 1!?
im kind of sure that i utilized position() is xslt to do just what i 
want to do here...

> 
(Continue reading)

Martin Honnen | 1 May 2006 17:49
Picon
Favicon

Re: document.evaluate and xpath function position()

Sebastian Fey wrote:

> <parent>
>  <child />
>  <child_with_other_tagname />
>  <child />
>  <child /><!--this is my context node-->
> </parent>
> 
> contextNode is the marked ELEMENT_NODE.
> I want to create a xpath like "//parent/child[3]"
> 
> 
>>
>> If you want to count the number of preceding-sibling or the number of
>> nodes on the ancestor-or-self axis you can certainly do that e.g.
>>   document.evaluate(
>>     'count(preceding-sibling::*) + 1',
>>     contextNode,
>>     null,
>>     XPathResult.NUMBER_TYPE,
>>     null
>>   )
> 
> 
> this would return 4, because child_with_other_tagname is counted as a 
> sibling.

If you don't want that then use the XPath expression
   count(preceding-sibling::child) + 1
(Continue reading)

Jonas Sicking | 2 May 2006 03:03

Re: document.evaluate and xpath function position()

> xpathResult == 1, no matter which position contextNode has.
> It seems like evaluate() does not see the current context node-set, but 
> takes contextNode as the current context node-set.
> Or do i miss anything?

The thing is that the current context node-set is a node-set containing 
just the context node. Which obviously is always going to produce the 
position 1.

Note that the current context node-set doesn't neccesarily have to be 
the siblings of the context node, though it often is.

/ Jonas
Sebastian Fey | 2 May 2006 11:48
Picon

Re: document.evaluate and xpath function position()

Jonas Sicking schrieb:
>> xpathResult == 1, no matter which position contextNode has.
>> It seems like evaluate() does not see the current context node-set, 
>> but takes contextNode as the current context node-set.
>> Or do i miss anything?
> 
> The thing is that the current context node-set is a node-set containing 
> just the context node. Which obviously is always going to produce the 
> position 1.
> 
> Note that the current context node-set doesn't neccesarily have to be 
> the siblings of the context node, though it often is.
> 
> / Jonas

hi,

ok i understand.
wouldnt it be nice to tell evaluate() the contextNode _and_ the context 
node set. is there a way to do that?

greeetings,
Sebastian
Tedsec | 2 May 2006 15:48
Picon
Favicon

Re: DOM parsing & unclosed meta tags

Thank you. That's a bummer. But thanks for the heads up.
Tedsec | 2 May 2006 16:07
Picon
Favicon

new document element

Here is my scenario and question:
I have a javascript that runs on page A. It opens a new tab and loads
page B. I then want to use DOM methods to manipulate page B. However,
when I make calls such as "document.getElementById()" the results
returned reflect the tags on page A. When a script called from page A
opens another page, how can that script reference the DOM of the new
page?

Here are the lengthy details:
I am developing a firefox extension which takes some time to load. As a
result, I would like to open a new tab and manipulate the DOM structure
to add notifications so that the user knows what's going on. My code so
far looks like this:

var browser = document.getElementById( "content" );
var tab =
browser.addTab("chrome://searchstatus/content/backlinkstatus.html");
browser.selectedTab = tab;
var sourceurl = document.getElementById("sourceurl").firstChild;
sourceurl.data="I changed you!!"

The new tab was added correctly, however I kept getting the error that
"sourceurl" had no attributes. I tested the last two lines as an inline
script in the backlinkstatus.html page itself, and they worked just
fine, so I wondered what the problem was. I then used the following
script to identify the problem:

var browser = document.getElementById( "content" );
var tab =
browser.addTab("chrome://searchstatus/content/backlinkstatus.html");
(Continue reading)

Jonas Sicking | 2 May 2006 17:42

Re: document.evaluate and xpath function position()

> ok i understand.
> wouldnt it be nice to tell evaluate() the contextNode _and_ the context 
> node set. is there a way to do that?

We have added the function evaluateWithContext on the XPathExpression 
interface which lets provide a size and position when evaluating the 
expression.

However the size and position is provided as two numbers, so it's 
probably not what you want.

What is the use case for providing the context node set as a set of 
nodes? I.e. what do you need it for?

/ Jonas
Jonas Sicking | 2 May 2006 17:44

Re: DOM parsing & unclosed meta tags

> There is currently no API to parse a response or a string as HTML 
> (text/html).

That's not entierly true, is it. You can take the .responseText and pass 
it to document.write or .innerHTML. Neither are very ideal, but they 
should work.

/ Jonas

Gmane