dsubq24666 2015-11-05 04: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 04: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.

    展开全部

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

报告相同问题?

悬赏问题

  • ¥15 HDL串口设计debug
  • ¥15 设计出USBDM硬件实物
  • ¥15 复现图像修复算法PD-GAN模型(基于概率多样性GAN)
  • ¥15 信息管理系统的排序和查找
  • ¥15 vscode请求各位指导
  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部