Search code examples
phphtmldomdomdocumentdomxpath

Select Class Name value in DOMXPath


Possible Duplicate:
How to extract a node attribute from XML using PHP’s DOM Parser

I have HTML like this:

   <tr class="calendar_row" data-eventid="48256">
   ...
   </tr>

I just want to select the value or data-eventid across the web page but I don't have any idea how to do it in xpath. Is it possible?


Solution

  • You can use @ to get at attributes.

    //assume $dom is a DOMDocument
    
    $finder = new DOMXPath($dom);
    //  //tr[@data-eventid] = all tr nodes that have a data-eventid attribute
    //    /@data-eventid = the attribute node itself as opposed to the tr node
    $nodes = $finder->query('//tr[@data-eventid]/@data-eventid');
    
    foreach($nodes as $node) {
        echo $node->nodeValue."\n"; // echos your data-eventid value ie. 48256
    }