dom - PHP DomXPath to find nested element value -


i'm using php dom extract data page , having hard time getting href value nested element using domxpath.

here's html:

<span class="myclass">     <a href="/relative/path">my value</a>     <span class="otherclass"></span> </span> 

and here's xpath query:

$xpath = new domxpath($dom); $classname = "myclass"; $nodes = $xpath->query("//span[contains(@class, '$classname')]");  foreach ($nodes $node){     echo $node->nodevalue;     echo ",";     echo $node->getattribute('href');     echo "<br>"; } 

i'm able nodevalue fine ('my value'), not value of href. i'm sure i'm missing , not understanding this. need separate query href value? what's best way this?

in loop, $node span because xpath selecting span elements given class, that's why doesn't have href.

if want select anchor under span, change to:

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

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -