S
S
Shimpanze2019-07-25 06:38:23
XPath
Shimpanze, 2019-07-25 06:38:23

How to get and immediately merge the next (after the current) elements?

Hello!
There is a markup:

<div class="abc">
  <span class="foo">Hello</span>
  <span class="bar">World</span>
  <span class="baz">by</span>
</div>

span.fooHow to get the text content of the next two ( span.barand span.baz) by pushing from the element .
The output should be one line: "World by".
I write like this:
div/span[@class="foo"]//concat(following-sibling::*[1], following-sibling::*[2])

It works in a sandbox, but not in a real project. Since you cannot directly pass a function to a string concat(). When I wrap it in square brackets, it doesn't work either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2019-07-25
@Shimpanze

<?php

$dom = new DOMDocument();
$dom->loadXML('
    <div class="abc">
        <span class="foo">Hello</span>
        <span class="bar">World</span>
        <span class="baz">by</span>
    </div>');

$xpath = new DOMXPath($dom);
$result = $xpath->evaluate('concat(/div/span[@class="foo"]/following-sibling::*[1], " ", /div/span[@class="foo"]/following-sibling::*[2])');

var_dump($result);

Result:
string 'World by' (length=8)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question