在使用XPath从XML文档提取数据时遇到问题


Trouble extracting data from an XML document using XPath

我试图从xpath "//otherManagers2Info/otherManager2/otherManager"中提取所有的"name"answers"form13FFileNumber"值:https://www.sec.gov/Archives/edgar/data/1067983/000095012314002615/primary_doc.xml

这是我的代码。知道我哪里做错了吗?
$xml = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadXML($xml);
$x = new DOMXpath($dom);
$other_managers = array();
$nodes = $x->query('//otherManagers2Info/otherManager2/otherManager');
if (!empty($nodes)) {
    $i = 0;
    foreach ($nodes as $n) {
        $i++;
        $other_managers[$i]['form13FFileNumber'] = $x->evaluate('form13FFileNumber', $n)->item(0)->nodeValue;
        $other_managers[$i]['name'] = $x->evaluate('name', $n)->item(0)->nodeValue;
    }
}

就像您在评论中发布的那样,您可以使用自己的Xpath前缀注册名称空间。命名空间前缀只是别名。Xpath中没有默认的名称空间,因此您必须注册并使用前缀。

然而,表达式总是返回一个可遍历的节点列表,您可以使用foreach来迭代它们。query()evaluate()以上下文节点作为第二个参数,表达式是相对于上下文的。最后一个evaluate()可以直接返回标量值。如果将Xpath中的节点列表强制转换为标量类型(如字符串)或使用count()之类的函数,就会发生这种情况。

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('e13', 'http://www.sec.gov/edgar/thirteenffiler');
$xpath->registerNamespace('ecom', 'http://www.sec.gov/edgar/common');
$result = [];
$nodes = $xpath->evaluate('//e13:otherManagers2Info/e13:otherManager2/e13:otherManager');
foreach ($nodes as $node) {
  $result[] = [
    'form13FFileNumber' => $xpath->evaluate('string(e13:form13FFileNumber)', $node),
    'name' => $xpath->evaluate('string(e13:name)', $node),
  ];
}
var_dump($result);

演示:https://eval.in/125200