1

I'm trying to extract all of the "name" and "form13FFileNumber" values from xpath "//otherManagers2Info/otherManager2/otherManager" in this document: https://www.sec.gov/Archives/edgar/data/1067983/000095012314002615/primary_doc.xml

Here is my code. Any idea what I am doing wrong here?

$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;
    }
}
eComEvo
  • 11,669
  • 26
  • 89
  • 145

1 Answers1

0

Like you posted in the comment you can just register the namespace with an own prefix for Xpath. Namespace prefixes are just aliases. Here is no default namespace in Xpath, so you always have to register and use an prefix.

However, expressions always return a traversable node list, you can use foreach to iterate them. query() and evaluate() take a context node as the second argument, expression are relative to the context. Last evaluate() can return scalar values directly. This happens if you cast the node list in Xpath into a scalar type (like a string) or use function like 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);

Demo: https://eval.in/125200

ThW
  • 19,120
  • 3
  • 22
  • 44