dtgv52982 2018-03-13 02:35
浏览 31
已采纳

PHP / XPATH - 找到父母的先知和兄弟,并获得其“儿童”

I've been trying to figure this our for days, but I simply cannot seem to get it to work.

Let's say I have an XML file named test.xml like this:

<root>
    <itemList>
        <item>
            <name>A</name>
            <type>AAA</type>
        </item>
        <item>
            <name>B</name>
            <type>BBB</type>
        </item>
        <item>
            <name>C</name>
            <type>CCC</type>
        </item>
    </itemList>
</root>

From PHP, I use SimpleXMLElement to find the node with text BBB.

<?php 
$xmlStr = file_get_contents('test.xml');
$xml = new SimpleXMLElement($xmlStr);
$res = $xml->xpath('//type[contains(text(), "BBB")]/parent::*');

echo "{$res[0]->name} ({$res[0]->type})";
// Result: B (BBB)

Now, I'd like to find the preceding-sibling node of the parent, and get the child nodes' values like A (AAA), but I simply can't figure out how to do so.

Any help would be great.

Thanks.

  • 写回答

2条回答 默认 最新

  • dongyi5817 2018-03-13 03:48
    关注

    To get the nearest preceding sibling, use this XPath query:

    //type[contains(text(), "BBB")]/parent::item/preceding-sibling::item[1]
    

    You need to set the predicate to 1 so as to pick the nearest of the siblings. Otherwise you'd always get the first sibling (for example, if you remove the [1] predicate, you'll get the AAA element for both BBB and CCC)

    Note that the wildcards are not necessary since you presumably already know what the tags are.

    $xml = "<root>
        <itemList>
            <item>
                <name>A</name>
                <type>AAA</type>
            </item>
            <item>
                <name>B</name>
                <type>BBB</type>
            </item>
            <item>
                <name>C</name>
                <type>CCC</type>
            </item>
        </itemList>
    </root>";
    
    $xml = new SimpleXMLElement($xml);
    
    $res = $xml->xpath('//type[contains(text(), "BBB")]/parent::item/preceding-sibling::item[1]');
    echo "{$res[0]->name} ({$res[0]->type})".PHP_EOL;
    
    $res = $xml->xpath('//type[contains(text(), "CCC")]/parent::item/preceding-sibling::item[1]');
    echo "{$res[0]->name} ({$res[0]->type})";
    

    Demo

    Result

    A (AAA)
    B (BBB)

    To further illustrate the need to use the predicate, take a look at this:

    $xml = "<root>
        <itemList>
            <item>
                <name>A</name>
                <type>AAA</type>
            </item>
            <item>
                <name>B</name>
                <type>BBB</type>
            </item>
            <item>
                <name>C</name>
                <type>CCC</type>
            </item>
            <item>
                <name>C</name>
                <type>DDD</type>
            </item>
        </itemList>
    </root>";
    
    $xml = new SimpleXMLElement($xml);
    
    $res = $xml->xpath('//type[contains(text(), "DDD")]/parent::item/preceding-sibling::item');
    var_dump($res);
    

    Result

    array (size=3)
      0 => 
        object(SimpleXMLElement)[2]
          public 'name' => string 'A' (length=1)
          public 'type' => string 'AAA' (length=3)
      1 => 
        object(SimpleXMLElement)[3]
          public 'name' => string 'B' (length=1)
          public 'type' => string 'BBB' (length=3)
      2 => 
        object(SimpleXMLElement)[4]
          public 'name' => string 'C' (length=1)
          public 'type' => string 'CCC' (length=3)
    

    See how, no matter which element you select with the query, the farthest sibling is always first in the list (and the closest the last)? So, to simulate using the predicate, you could also get the closest sibling simply picking the last element in the array (notice there's no [1] predicate):

    $res = $xml->xpath('//type[contains(text(), "DDD")]/parent::item/preceding-sibling::item');
    $total = count($res);
    echo "{$res[$total - 1]->name} ({$res[$total - 1]->type})".PHP_EOL;
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题