php - XPATH Get Attribute of Current Node -
having trouble getting attribute of current node in php , making condition based on attribute...
example xml
<div class='parent'> <div class='title'>a title</div> <div class='child'>some text</div> <div class='child'>some text</div> <div class='title'>a title</div> <div class='child'>some text</div> <div class='child'>some text</div> </div>
what trying traverse xml in php , different things based on class of element/node
eg.
$doc->loadhtml($xml_string); $xpath = new domxpath($doc); $nodelist = $xpath->query("//div[@class='parent']/div"); foreach ($nodelist $node) { if (current div node attribute equals title) { set $title variable text() of current node } elseif(current div node attribute equals child){ set $child variable text() of current node } }
i've tried kind of things following...
if ($xpath->query("./[@class='title']/text()",$node)->length > 0) { }
but keep getting php errors saying xpath syntax not valid. can me?
you can achieve using getattribute()
method. example:
foreach($nodelist $node) { $attribute = $node->getattribute('class'); if($attribute == 'title') { // } elseif ($attribute == 'child') { // } }
Comments
Post a Comment