S
S
Shimpanze2019-01-14 06:30:46
XPath
Shimpanze, 2019-01-14 06:30:46

Why doesn't the xpath check work?

Good afternoon!
It is necessary in a loop (when iterating over elements) to check whether the element fits the xpath condition.
In this case, does the class contain foo.
If it contains - skip it, if not - add it to the variable.
I do it like this:

$content = '';

foreach($entry as $element) {
  // Вот здесь я спрашиваю, текущий элемент в цикле
  // подходит под условие xpath?
  if ($xpath->evaluate('self::div[contains(@class, \'foo\')]', $element) === true) {
    continue;
  }
  else {
    $content .= $dom->saveHTML($element);
  }
}

echo "$content";

Why do I get an empty result in the output?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-01-14
@Shimpanze

$entry = $xpath->query('//div[contains(@id, \'entry\')]/div');

$content = '';
foreach($entry as $element) {
  if(! $xpath->evaluate("contains(@class, 'foo')", $element)) {
    $content .= $dom->saveHTML($element) . PHP_EOL;
  }
}
echo $content;

//<div class="world bar">bla bla bla</div>
//<div class="two bar">bla bla bla</div>
//<div class="paper bar">bla bla bla</div>
//<div class="boo bar loo">bla bla bla</div>
https://3v4l.org/FhNUp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question