0

I know this is a commonly asked question (I found Concatenate multiple node values in xpath XPath joining multiple elements and a few others), but for the life of me I can't figure it out. I've got the following HTML:

<div class="product-card__price__new"><span class="product-card__price__euros">0.</span> <span class="product-card__price__cents">69</span></div>

From which I need to extract the 0.69. I tried the following XPATH:

'.//*[@class="product-card__price__new"]/concat(/span/text(), following-sibling::span[1]/text)'
'.//*[@class="product-card__price__new"]/span/text()/concat(., following-sibling::span[1]/text)'
'.//*[@class="product-card__price__new"]/text()'

But I keep getting nothing. What would be the correct expression to extract it?

1 Answers1

0

It's much simpler than this. The string value of an element is the concatenation of all its descendant text nodes. So it's just

string(.//*[@class="product-card__price__new"])

and in many contexts you don't need the call on string() because it's implicit.

Most of the time when people use text() they are (a) over-complicating things and/or (b) getting it wrong.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks! I'll update my other text() selectors as well. – Jasper Wijnhoven Apr 21 '21 at 10:51
  • Your suggestion actually returns `0. 69` because of the space between the two span-nodes. I would suggest `.//*[@class="product-card__price__new"]/string-join(span)` instead. – Reino Apr 24 '21 at 19:24