dozabg1616 2013-05-20 12:45
浏览 64

使用xpath和php获取子元素[关闭]

I have this sample xml file:-

<products>
   <product_id value="1">
       <tab_id value="251">
            <dist_region value="5" />
            <dist_region value="10066" />
            <dist_region value="10069" />
       </tab_id>
   </product_id>
</products>

I am trying to get tab_id child element using XPathand I want result in set of child elements.

My expected output is as follows:-

dist_region,dist_region,dist_region

MY XPATH:-

$tab = $product->xpath('//tab_id/*');

Can anyone suggest what is the XPath to get child elements? Thanks.

  • 写回答

2条回答 默认 最新

  • dua27031 2013-05-20 13:02
    关注

    Your XPath should be based on specific condition like value (if it is <tab_id id='256'> then more readable)

    $result = $product->query('//tab_id[@value=' . $id . ']/*');
    

    and then loop through it like below

    if($result->length){
        foreach ($result as $item):
            echo "Value: ".$item['value'];
        echo "<hr>";
    endforeach;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!