How do I select the last XHTML <span> element with a particular class in XPath?

Posted by Fintan on Stack Overflow See other posts from Stack Overflow or by Fintan
Published on 2010-12-27T13:48:05Z Indexed on 2010/12/27 13:54 UTC
Read the original article Hit count: 361

Filed under:
|
|
|

My target XHTML document (simplified) is like the following:

<html>
<head>
</head>
<body>
<span class="boris"> </span>
<span class="boris"> </span>
<span class="johnson"> </span>
</body>
</html>

I'm trying to select the last of class "boris."

The XPath expression

//span[@class="boris"]

selects all spans of class boris. How do I select the last one of these?

I've tried

//span[@class="boris" and last()]

which doesn't work because last() here refers to the last span in the WHOLE DOCUMENT.

How do I select all the spans of class boris... and then the last one of these?

I've read 5 or 6 XPath tutorials and done a lot of Googling and I can't find a way to do this in XPath alone :(

Thanks in advance for help :)

© Stack Overflow or respective owner

Related posts about html

Related posts about Xml