xpath help

Y

Yansky

Hi, I'm trying to check if there is a next page link on pages. Here is
an example page: http://forums.whirlpool.net.au/forum-replies.cfm?t=977718&p=42

This is the xpath I'm trying:

document.evaluate( '//ul[@class= "pagination"][1]//li[@class=
"current"][following-sibling::li][1][not( @class = "last" )]/
a' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE,
null ).singleNodeValue;

but for some reason it's returning the < a> from the < li> with the
class of "current" rather than the next page's < a> (which is
http://forums.whirlpool.net.au/forum-replies.cfm?t=977718&p=43 )

Can anyone show me where I've gone wrong?
Cheers.
 
R

RobG

Hi, I'm trying to check if there is a next page link on pages. Here is
an example page:http://forums.whirlpool.net.au/forum-replies.cfm?t=977718&p=42

It would have been better if you'd posted a minimial example that
trims out the irrelevant parts.
This is the xpath I'm trying:

document.evaluate( '//ul[@class= "pagination"][1]//li[@class=
"current"][following-sibling::li][1][not( @class = "last" )]/
a'

That expression doesn't look quite right, does it return the right
nodes? Breaking it down:

//ul[@class= "pagination"][1]

Returns the first UL in the document with a class of pagination...

//li[@class="current"]

Gets all of the LI descendants of the UL with a class of "current"...

[following-sibling::li]

Returns all of the LI elements that have a following LI element
sibling (not necessarily the next sibling). That expression seems
redundant: the siblings of an LI can only ever be LI or #text nodes.
Essentially, it gets all but the last LI (since the last one can't
have a following LI sibling by definition, otherwise it won't be
last).

[1]

Get the first of the above set, effetively the [following-sibling::li]
expression can be removed. If there is only one LI child, it wont
select any elements.

[not( @class = "last" )]

You've already selected the first one, if it has a class of "last",
you just filtered it out, I think you want it the other way around,
i.e.:

[not( @class = "last" )][1]


/a

Get all the A child nodes.

It seems to me that you want to see if the LI with a class of current
is the last one before the LI with a class of 'last', is that right?
In that case, why not (wrapped for posting):

//ul[@id="top_pagination"]/li[not (@class="last")]
[position()=last()][@class="current"]


If a node is returned, then it's the current page LI. If you want to
get the next page LI, then use [not(@class="current"0)]. This is
heavily dependent on the structure of the document.
 
R

RobG

RobG wrote:

[...]
It seems to me that you want to see if the LI with a class of current
is the last one before the LI with a class of 'last', is that right?
In that case, why not (wrapped for posting):
//ul[@id="top_pagination"]/li[not (@class="last")]
  [position()=last()][@class="current"]

Last time I checked, position() was pretty buggy in WebKit (which means
that, at least, Safari and Chrome are affected). I wouldn't use it for
general web.

Yes, I'd use DOM, maybe the OP will come to the same conclusion. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top