David Corp
David Corp

Reputation: 506

unable to correctly apply XPath from php

I'm using xpath to extract data from a web site, but I have a problem with the XPath selector, assuming i have this HTML code:

<div id="_parent">
  <div class="my">
      Hi!
      <p>I am a child!</p>
      <span class="someclass">I am a <b>span</b> child!</span>
  </div>
</div>

what I get:

Hi!
I am a child!
I am a span child!

what I should get:

<div class="my">
    Hi!
    <p>I am a child!</p>
    <span class="someclass">I am a <b>span</b> child!</span>
</div>

My current xpath php code

$xpath = new DOMXPath($doc);
$entries = $xpath->query("//div[@class='my']");

Upvotes: 0

Views: 48

Answers (1)

When in Chrome I open the console and enter this in it:

document.evaluate( "//div[@class='my']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

then what I get is:

<div class="my">
    Hi!
    <p>I am a child!</p>
    <span class="someclass">I am a <b>span</b> child!</span>
</div>

so the XPath expression actually works as intended. So I infer, that the way you apply the XPath expression must be wrong. However you did not show us the code that applies the XPath expression?

Upvotes: 1

Related Questions