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 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条