0

I'm an xPath and PHP newbie. I want to extract the attribute values from a tag

<a onclick="sell_this_form('24042', '195'); return false;" href="javascript:void(0);">Sell</a>

I want to extract the first argument to sell_this_form ie 24042. I've built an expression that ends this way

.../a/@onclick

It evaluates properly on my Xpath expression builder. However when I dump my result in PHP using var_dump(), I get a blank node. How can I get it to give me what I need? I've tried

$xpath->query("//input[@name]/@name")->value;

as suggested on this site. However, I get an error. What am I doing wrong? Relevant sections of my code follow

$dom = new DOMDocument;
$prev_err = libxml_use_internal_errors(TRUE);
$dom->loadHTML($page);
libxml_clear_errors();
libxml_use_internal_errors($prev_err);
$xpath = new DOMXPath($dom);
$entries = $xpath->query($xpathExpr);
//$entries = $xpath->query($xpathExpr)->value;
if (!$entries)
    throw new Exception("XPath evaluation error");

foreach ($entries as $entry)
    echo "<br/>value: {$entry->nodeValue}";
user1801060
  • 2,733
  • 6
  • 25
  • 44
  • Have you tried `$xpath->query('//a[@onclick]/@onclick')->value`? – lonesomeday Nov 05 '13 at 20:51
  • I've tried that and I get an error. When I remove ->value I get a blank node. – user1801060 Nov 05 '13 at 21:01
  • 1
    query() returns a DOMNodeList. DOMNodeLists don't have a value property. Also, var_dump doesnt work on most DOM objects because they dont expose their internals. – Gordon Nov 05 '13 at 21:04
  • The value attribute is commented out. I still get a blank node when I ran query without the value property. – user1801060 Nov 05 '13 at 21:06
  • You dont get a [node](http://php.net/DOMNODE). You get a [DOMNodeList](http://php.net/DOMNODELIST). – Gordon Nov 05 '13 at 21:09
  • Yes, I get class DOMNodeList#3 (0) {} – user1801060 Nov 05 '13 at 21:10
  • 1
    Which tells you nothing because like I already said: var_dump doesnt work on most DOM objects. Refer to the docs to see how to access a DOMNodeList. Or see the linked dupe on how to access a DOMNodeList to get the DOMNode and it's value. The final code example has all you need. – Gordon Nov 05 '13 at 21:14

0 Answers0