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?