PHP XML - 找出已知值的路径

时间:2014-01-05 00:07:56

标签: php xml parsing xpath xml-parsing

这是一个XML位:

[11] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 46e8f57e67db48b29d84dda77cf0ef51
                                            [label] => Publications
                                        )

                                    [section] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 9a34d6b273914f18b2273e8de7c48fd6
                                                            [label] => Journal Articles
                                                            [recordId] => 1a5a5710b0e0468e92f9a2ced92906e3
                                                        )

我知道值“46e8f57e67db48b29d84dda77cf0ef51”,但它的位置因文件而异。我可以使用XPath来查找此值的路径吗?如果不是可以使用什么?

不起作用的最新试验:

$search = $xml->xpath("//text()=='047ec63e32fe450e943cb678339e8102'");

while(list( , $node) = each($search)) {

    echo '047ec63e32fe450e943cb678339e8102',$node,"\n";

}

2 个答案:

答案 0 :(得分:3)

PHPs DOMNode对象具有以下功能:DOMNode::getNodePath()

$xml = <<<'XML'
<root>
  <child key="1">
    <child key="2"/>
    <child key="3"/>
  </child>
</root>
XML;

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

$nodes = $xpath->evaluate('//child');

foreach ($nodes as $node) {
  var_dump($node->getNodePath());
}

输出:

string(11) "/root/child"
string(20) "/root/child/child[1]"
string(20) "/root/child/child[2]"

SimpleXML是DOM的包装器,这是一个允许您获取SimpleXMLElement的DOMNode的函数:dom_import_simplexml

$xml = <<<'XML'
<root>
  <child key="1">
    <child key="2"/>
    <child key="3"/>
  </child>
</root>
XML;

$structure = simplexml_load_string($xml);
$elements = $structure->xpath('//child');

foreach ($elements as $element) {
  $node = dom_import_simplexml($element);
  var_dump($node->getNodePath());
}

可以使用按属性xpath获取元素。

在文档中的任何位置使用元素joker选择所有节点:

//*

按id属性过滤它们:

//*[@id = "46e8f57e67db48b29d84dda77cf0ef51"]

$dom = new DOMDocument();
$dom->loadXml('<node id="46e8f57e67db48b29d84dda77cf0ef51"/>');
$xpath = new DOMXpath($dom);

foreach ($xpath->evaluate('//*[@id = "46e8f57e67db48b29d84dda77cf0ef51"]') as $node) {
  var_dump(
    $node->getNodePath()
  );
}

答案 1 :(得分:2)

此字符串是否始终位于 @id属性中?然后,有效且不同的路径始终为//*[@id='46e8f57e67db48b29d84dda77cf0ef51'],无论它在何处。

要构造给定节点的路径,请使用$node->getNodePath(),它将返回当前节点的XPath表达式。同时考虑this answer on constructing XPath expression using @id attributes, similar to like Firebug does,帐户。

对于SimpleXML,您必须手动完成所有操作。如果需要支持属性和其他路径,则必须添加此代码,此代码仅支持元素节点。

$results = $xml->xpath("/highways/route[66]");

foreach($results as $result) {
    $path = "";
    while (true) {
        // Is there an @id attribute? Shorten the path.
        if ($id = $result['id']) {
            $path = "//".$result->getName()."[@id='".(string) $id."']".$path;
            break;
        }
        // Determine preceding and following elements, build a position predicate from it.
        $preceding = $result->xpath("preceding-sibling::".$result->getName());
        $following = $result->xpath("following-sibling::".$result->getName());
        $predicate = (count($preceding) + count($following)) > 0 ? "[".(count($preceding)+1)."]" : "";
        $path = "/".$result->getName().$predicate.$path;
        // Is there a parent node? Then go on.
        $result = $result->xpath("parent::*");
        if (count($result) > 0) $result = $result[0];
        else break;
    }
    echo $path."\n";
}