S
S
Shimpanze2017-03-27 03:32:51
XPath
Shimpanze, 2017-03-27 03:32:51

XPath: how to find a node?

Good afternoon!
How to find direct descendants of an element (including text nodes containing inline tags) of an element?
An example example was:

<div id="orion">
  Lorem ipsum <i>dolor</i> sit amet.
  Lorem <b>ipsum</b> dolor sit amet.

  <p>Lorem ipsum dolor sit amet.</p>

  <span>Lorem ipsum dolor sit amet.</span>
</div>

An example would become:
<div id="orion">
  <p>
    Lorem ipsum <i>dolor</i> sit amet.
    Lorem <b>ipsum</b> dolor sit amet.
  </p>

  <p>Lorem ipsum dolor sit amet.</p>

  <p>
    <span>Lorem ipsum dolor sit amet.</span> Lorem ipsum dolor sit <span>amet</span>.
  </p>
</div>

I use a function with the following axis:
$dom = new DOMDocument;
$dom->loadHTML( '$html' );
$xpath = new DOMXPath( $dom );

foreach ( $xpath->query( '//div/text()[i][b][span] | //div/node()' ) as $textNode ) {
  echo trim( $textNode->nodeValue ), PHP_EOL;
}

Doesn't work in any. Although, according to the documentation, I speak Russian to him in black, - I allow you, they say, to use text nodes that have lowercase tags: text()[i][b][span].
How to make it work?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2017-03-30
@Shimpanze

what you need, I did not understand, but look at this

('//div[@class="class-name"][h3[a[text()="'+text+'"]]]/following-sibling::small/strong/text()')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question