Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine two nodes into one group in Xpath?

Tags:

html

xml

xpath

I have html like this:

<div>
    <div class="a">10</div>
    <div class="b">11</div>
    <div class="a">20</div>
    <div class="b">21</div>

</div>

Sometimes I use //div[@class="a" or @class="b"] to get four groups: [10, 11, 20, 21].

But this time I want to get something like this, two groups: ["10 11", "20 21"], could I?

I mean that how to combine two nodes(class a, class b) into one group?

like image 647
Kxrr Avatar asked Oct 23 '25 16:10

Kxrr


1 Answers

This would be a working XPath 2.0 solution (running example):

//div[@class="a"]/concat(
    text(), 
    ' ', 
    string-join((following-sibling::div[@class="b"]/text())[1], '')
)

I don't think a solution with XPath 1.0 would be possible here.

like image 73
Lukas Eder Avatar answered Oct 25 '25 05:10

Lukas Eder