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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题