1

I'm using the PHP DOM to extract data from a page and having a hard time getting the href value for a nested element using DomXPath.

Here's my html:

<span class="myclass">
    <a href="/relative/path">My Value</a>
    <span class="otherclass"></span>
</span>

And here's my XPath query:

$xpath = new DomXPath($dom);
$classname = "myclass";
$nodes = $xpath->query("//span[contains(@class, '$classname')]");

foreach ($nodes as $node){
    echo $node->nodeValue;
    echo ",";
    echo $node->getAttribute('href');
    echo "<br>";
}

I'm able to get the nodeValue just fine ('My Value'), but not the value of the href. I'm sure I'm missing something and not understanding this. Do I need a separate query to get the href value? What's the best way to do this?

1 Answer 1

1

In your loop, $node is the span because your XPath is selecting span elements with the given class, that's why it doesn't have a href.

If you want to select the anchor that is under the span, change to:

$nodes = $xpath->query("//span[contains(@class, '$classname')]/a");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.