Is there a way to get the text between 2 nodes with XPath 1 ?
Example: We want to get text between F and D and the expected result would be "G"
$html = ''.
'<html>'.
'<body>'.
'<a>A</a>'.
'<b>B
<c>C
<F>F</F>
</c>
<G>G</G>
</b>'.
'<d>D
<e>E</e>
</d>'.
'</body>'.
'</html>';
Here is the query:
$dom = new \DOMDocument();
@$dom->loadHTML($html);
$xpath = new \DOMXPath($dom);
$a = '/html/body/b/c/f';
$b = '/html/body/d';
$nodesBetween = getNodesBetween($a,$b, $xpath);
Finally the function:
public function getNodesBetween($a, $b, $domxpath) {
$query = $a."/following::text()[. = ".$b."/preceding::text()]";
$elements = $domxpath->query($query);
$inside = '';
foreach ($elements as $element) {
$inside .= $element->nodeValue;
}
dd($inside);
}
If I try to search from A to D, it's working and the output is "B C F G". If I search between F and D, it's returning an empty string. Seems it's searching for siblings and as F has none, it stops. The only answer I could find was with XPath 2.0:
"assuming you want nodes at all tree depths between the two h3 elements, which would not necessarily be siblings"
from https://stackoverflow.com/a/3838151/3628541
/path/to/first/h3/following::node()[. << /path/to/second/h3]
What is the equivalent in 1.0 ?