12

I'm having some trouble understanding what exactly is stored in childNodes. Ideally I'd like to do another xquery on each of the child nodes, but can't seem to get it straight. Here's 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 element of "something" (i.e., $n) I want to access the values of the two pieces of text and the href. I tried using childNode and another xquery but couldn't get anything to work. Any help would be greatly appreciated!

Bryan
  • 1,771
  • 4
  • 17
  • 30

4 Answers4

14

Yes you can run another xpath query, something like that :

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

    echo $other_nodes->length;
}

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

mravey
  • 4,380
  • 2
  • 21
  • 31
  • Thanks @TrexXx, but when I use "$other_nodes->nodeValue" I don't see anything. Is that not starting back at the root element? I originally thought it would be something like this: `$other_nodes = $n->query('div[@class="somethingelse"]')`; – Bryan Oct 26 '11 at 00:06
  • `$other_nodes` is a node list, so first you will have to get an item (a node) and then get its value. Something like `$other_nodes->item(0)->nodeValue`. – mravey Oct 26 '11 at 00:10
  • `$other_nodes` ends up containing information from outside of `$n` (i.e. from other "something" elements); it shouldn't be the way, right? Do you know of any other approach that doesn't require going back to query the root element? – Bryan Oct 26 '11 at 00:23
  • I don't really understand what you are trying to do ? – mravey Oct 26 '11 at 00:43
  • I'm trying to access (or do searches on) only the information contained within each of the original nodes (that is, the "something" elements that are looped through with `$nodelist`). I want to do this incase some of the elements have different or incomplete sub-information. Thanks for working through this with me. – Bryan Oct 26 '11 at 01:37
  • This answer got me to the solution I was looking for after I also referenced this: [http://stackoverflow.com/questions/11471922/php-xpath-how-to-get-two-information-in-a-child-node](http://stackoverflow.com/questions/11471922/php-xpath-how-to-get-two-information-in-a-child-node). Check out my answer for an example. – Will Schoenberger Aug 10 '14 at 07:07
5

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

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 it's just enough to combine queries using the // path expression for narrowing your search. The // path expression selects nodes in the document starting from the current node that match the selector.

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

Then loop through those for the stuff you need. Hope this helps.

5

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;
}
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

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

$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);`
Temi Fakunle
  • 672
  • 6
  • 7