dsubq24666 2015-11-05 12:36
浏览 49
已采纳

检查xml节点的父节点是否具有特定属性

I have the sample xml file below:

<?xml version="1.0" encoding="utf-8" ?>
<categories>
    <category id="1" name="Car">
        <category id="12" name="Electrical">
            <category id="18" name="On-Road">
                <category id="127" name="Carisma"></category>
                <category id="128" name="HPI - Maverick"></category>
                <category id="452" name="HSP"></category>
                <category id="130" name="Other"></category>
            </category>
            <category id="16" name="Off-Road">
                <category id="132" name="HPI - Maverick"></category>
                <category id="225" name="Carisma"></category>
                <category id="315" name="HSP"></category>
                <category id="420" name="Other"></category>
            </category>
        </category>
    </category>
</categories>

What I need is to all the parent nodes of any child node whit a specifi id value.

For example if the id value is 128, then it will echo: Car/Electrical/On-Road/HPI - Maverick

if the id value is 12, then it will echo Car/Electrical

and goes on...

I have managed to get the node with the specific id value (below) but I can't figure out how to get the parents chain from the selected node.

<?php
$file = "http://www.localhost.com/categories.xml";

$microline = new SimpleXMLElement($file, null, true);

foreach($microline as $cat){
    if ($cat->children['id'] = $catid) {

        $current_cat_name = $cat->xpath("//*[@id=$catid]/@name")[0];
        echo $current_cat_name;
    }
}
unset($microline);
?>
  • 写回答

1条回答 默认 最新

  • dsflxcfuw27742248 2015-11-05 12:42
    关注

    If you use the XPath //*[@id=$catid]/ancestor-or-self::category/@name then you have all attribute values and simply need to concatenate them with the slash character in PHP.

    So doing e.g.

    $xml = <<<'EOB'
    <categories>
        <category id="1" name="Car">
            <category id="12" name="Electrical">
                <category id="18" name="On-Road">
                    <category id="127" name="Carisma"></category>
                    <category id="128" name="HPI - Maverick"></category>
                    <category id="452" name="HSP"></category>
                    <category id="130" name="Other"></category>
                </category>
                <category id="16" name="Off-Road">
                    <category id="132" name="HPI - Maverick"></category>
                    <category id="225" name="Carisma"></category>
                    <category id="315" name="HSP"></category>
                    <category id="420" name="Other"></category>
                </category>
            </category>
        </category>
    </categories>
    EOB;
    
    $root = new SimpleXMLElement($xml);
    $id = 127;
    $cats = $root->xpath("//*[@id=$id]/ancestor-or-self::category");
    
    $path = implode('/', array_map(function($a) { return $a['name']; }, $cats));
    
    echo $path;
    

    would output Car/Electrical/On-Road/Carisma.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致