4

Is it possible to get to the second last element which is in this case "778-893-3551" using xpath without hardcoding any number in the end of the expression? I've tried with:

//section[@id="postingbody"]/text()

I don't wanna use something like this:

//section[@id="postingbody"]/text()[2]

Because, if any item is missing or the item differs from its number then the serial number used in the end of an expression doesn't bring any result.

Elements for the values within:

<section id="postingbody">Located in the heart of Guildford. Surrey, Built in 2014, looks and feels new. No GST. Still under 2/5/10 National New Home Warranty. Timeless Craftsman-style architecture, with wood and hardy plank exterior. Open floor plan with 9' ceiling on the main floor. 30" ceran top electric range self-cleaning oven and stainless steel 3 door fridge. Contemporary Real Maple cabinets. 2 Storey clubhouse with Gym, Sauna, Party hall &amp; Guest Suite. Easy access to No. walking distance to Guildford mall, library and all the services you desire, very convenient location, 8 minutes drive to a skytrain station, 2 minutes drive to hwy 1.<br>
2 covered parking.<br>
10 minutes drive from Coquitlam Centre.<br>
<br>
Call to make an appointment to view the place.<br>
<br>
Ahmed Afandi<br>
778-893-3551<br>
Kore Realty Central</section>
0

1 Answer 1

4

You can set position of selected element as last-1

//section[@id="postingbody"]/text()[position()=last()-1]
3
  • I never used such syntax :) . I need to read about, maybe, it depends on realization
    – splash58
    Commented May 5, 2017 at 10:56
  • //section[@id="postingbody"]/text()[last()-1] also worked for me Commented Apr 11, 2019 at 14:53
  • I tried something like Pavel Savine wrote but got empty value. Had to use position()= as splash58 told to get it working. Also, you might want //text() instead (double slash)
    – John Roe
    Commented May 23, 2021 at 8:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.