Moving child nodes using PHP DOMXpath?

I am having trouble understanding what exactly is stored in childNodes. Ideally, I would like to do another xquery on each of the child nodes, but it might not seem like it's straightforward. Here is my scenario: Data:

<div class="something"> <h3> <a href="link1.html">Link text 1</a> </h3> <div class"somethingelse">Something else text 1</div> </div> <div class="something"> <h3> <a href="link2.html">Link text 2</a> </h3> <div class"somethingelse">Something else text 2</div> </div> <div class="something"> <h3> <a href="link3.html">Link text 3</a> </h3> <div class"somethingelse">Something else text 3</div> </div> 

And the code:

 $html = new DOMDocument(); $html->loadHtmlFile($local_file); $xpath = new DOMXPath( $html ); $nodelist = $xpath->query( "//div[@class='something']"); foreach ($nodelist as $n) { Can I run another query here? } 

For each "something" element (i.e. $ n) I want to access the values ​​of the two parts of the text and href. I tried using childNode and another xquery but couldn't get it working. Any help would be greatly appreciated!

+6
source share
4 answers

Yes, you can run another xpath request, something like this:

 foreach ($nodelist as $n) { $other_nodes = $xpath->query('div[@class="somethingelse"]', $n); echo $other_nodes->length; } 

This will give you an inner div with the somethingelse class, the second argument of the request method $ xpath-> tells the request to accept this node as context, see more http://fr2.php.net/manual/en/domxpath.query.php

+10
source

Trexx had it, but he missed the last sentence of the question:

 foreach ($nodelist as $n){ $href = $xpath->query('h3/a', $n)->item(0)->getAttribute('href'); $a_text = $xpath->query('h3/a', $n)->item(0)->nodeValue; $div_text = $xpath->query('div', $n)->item(0)->nodeValue; } 
+3
source

If I understood your question correctly, it worked when I used the descendant expression ::. Try the following:

 foreach ($nodelist as $n) { $other_nodes = $xpath->query('descendant::div[@class="some-descendant"]', $n); echo $other_nodes->length; echo $other_nodes->item(0)->nodeValue; } 

Although sometimes this is enough to combine queries using the // path expression to narrow your search. The expression // path defines the nodes in the document, starting with the current node corresponding to the selector.

 $nodes = $xpath->query('//div[@class="some-descendant"]//div[@class="some-descendant-of-that-descendant"]'); 

Then loop the ones you need. Hope this helps.

+2
source

Here is a snippet of code that allows you to access the information contained in each node with the attribute of the "something" class:

 $nodes_tracker = 0; $nodes_array = array(); foreach($nodelist as $n){ $info = $xpath->query('//h3//a', $n)->item($nodes_tracker)->nodeValue; $extra_info = $xpath->query('//div[@class="somethingelse"', $n)->item($nodes_tracker)->nodeValue; array_push($nodes_array, $info. ' - '. $extra_info . '<br>'); //Add each info to array $nodes_tracker++; } print_r($nodes_array);` 
0
source

Source: https://habr.com/ru/post/900094/


All Articles