时间:2021-07-01 10:21:17 帮助过:14人阅读
[User:root Time:00:07:19 Path:/home/liangdong/php]$ php xpath.php type:small color:white age:5 [User:root Time:00:07:19 Path:/home/liangdong/php]$ cat xpath.phpEOF; $xml = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOBLANKS); $res = $xml->xpath("/pets/pet[type[@resource='small'] and color[@resource='white'] and age[@resource='5']]"); foreach ($res as $node) { $children = $node->children(); foreach ($children as $child) { echo $child->getName() . ":" . $child['resource'] . PHP_EOL; } } ?>
------解决方案--------------------
$str = <<EOF; $xml = simplexml_load_string($str); $res = $xml->xpath("/pets/pet[type[@resource='small'] and color[@resource='white'] and age[@resource='5']]"); echo $res[0]->attributes()->id; //02