PHP的XML - 找出路径为已知值(PHP XML - Find out the path to

2019-10-19 04:08发布

下面是一个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";

}

Answer 1:

PHP的对象的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的元件都可以使用。

选择使用元素百搭文档中任何地方的所有节点:

//*

由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()
  );
}


Answer 2:

这是串总是在@id属性 ? 然后一个有效和独特的路径总是//*[@id='46e8f57e67db48b29d84dda77cf0ef51']无论它在哪里。

为了构建到给定节点的路径,使用$node->getNodePath()将返回的XPath表达式为当前节点。 还拿上使用构建XPath表达式这个答案@id属性,类似于类似Firebug确实,在帐户。

对于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";
}


文章来源: PHP XML - Find out the path to a known value