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.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?