我正在尝试从本文档中的xpath“ // otherManagers2Info / otherManager2 / otherManager”中提取所有“名称”和“ 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()
以上下文节点作为第二个参数,则表达式是相对于上下文的。Lastevaluate()
可以直接返回标量值。如果将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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句