PHP xpath get tag name -
i have xml file this:
<js> <ba> <ea> <jd> <option>first sko</option> <option>second sko</option> <option>third sko</option> <option>fourth sko</option> <option>fifth sko</option> </jd> </ea> </ba> </js> i want retrieve tag name of grandparent tag (i.e. "ea") starting each value of "option" tag.
so 2 levels tag have tried:
$xmlitem = simplexml_load_file(thexmlfile.xml); foreach ($xmlitem->xpath('//jd/option') $juzg) { $cid = $xmlitem->xpath("name(//*[*[option = '" . $juzg . "']])"); $item['cid'] = (string)$cid; } the result when echo $cid or $item['cid'] "array" each loop.
i looking full script go in place of:
$cid = $xmlitem->xpath("name(//*[*[option = '" . $juzg . "']])"); i appreciate guidance on issue.
$xmlitem->xpath returns array, if (string)$xmlitem->xpath() 'array'
in example have iterate again on $cid or select $cid[0], don't think getting name of parent work.
either do:
foreach ($xmlitem->xpath('//jd/option') $juzg) { $cid = $xmlitem->xpath("//*[*[option = '".$juzg."']]"); $item['cid'] = $cid[0]->getname(); } however works if there isn't other option element in xml document same content, rather relying on content of element select parent nodes:
foreach ($xmlitem->xpath('//jd/option') $juzg) { $cid = $juzg->xpath('../..'); $item['cid'] = $cid[0]->getname(); } here can sure $cid have 1 object because select parent , element have 1 parent.
Comments
Post a Comment