Search code examples
phpxmlxpathxml-parsingxbrl

How to parse this XML using php


I have the following XML (XBRL) file

 <xbrli:context id="I2010_ForwardContractsMember">
 <xbrli:entity>
  <xbrli:identifier scheme="http://www.sec.gov/CIK">0000027419</xbrli:identifier>
  <xbrli:segment>
    <xbrldi:explicitMember dimension="us-gaap:DerivativeByNatureAxis">us-gaap:ForwardContractsMember</xbrldi:explicitMember>
  </xbrli:segment>
</xbrli:entity>
<xbrli:period>
  <xbrli:instant>2011-01-29</xbrli:instant>
</xbrli:period>
 </xbrli:context>
<xbrli:context id="D2010Q1">
  <xbrli:entity>
  <xbrli:identifier scheme="http://www.sec.gov/CIK">0000027419</xbrli:identifier>
  </xbrli:entity>
 <xbrli:period>
    <xbrli:startDate>2010-01-31</xbrli:startDate>
    <xbrli:endDate>2010-05-01</xbrli:endDate>
  </xbrli:period>
 </xbrli:context>

I want to get the startDate and endDate value when id="D2010Q1"

My codes is following

 $xml = new SimpleXMLElement($sec_file);
 $xml -> registerXPathNamespace('us-gaap', "http://fasb.org/us-gaap/2011-01-31");

  foreach ($xml->xpath('//xbrli:context') as $item) {

if ($item -> attributes() -> id == 'D2010Q1') {
    //print_r($item -> xpath('//xbrli:startDate'));
    echo $item->xpath('//xbrli:startDate');
    echo '<br>';
    break;
}
}

I don't know how to get startDate and endDate value. Any suggestions?

The original xml is located at

http://www.sec.gov/Archives/edgar/data/27419/000110465911031717/tgt-20110430.xml


Solution

  • Edit:

    Do I need to register all the namespace in the file? How could I get startDate and endDate infor?

    No you only have to register the namespaces you need i.e. http://www.xbrl.org/2003/instance

    $xmldoc = new DOMDocument();
    $xmldoc->load("http://www.sec.gov/Archives/edgar/data/27419/000110465911031717/tgt-20110430.xml");
    $xpath = new DOMXPath($xmldoc);
    $xpath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
    $nodelist = $xpath->query("/xbrli:xbrl/xbrli:context[@id='D2010Q1']/xbrli:period"); // much faster than //xbrli:context and //xbrli:startDate
    if($nodelist->length === 1)
    {
        $period = $nodelist->item(0);
        $nodelist = $xpath->query("xbrli:startDate", $period);
        $startDate = $nodelist->length === 1 ? $nodelist->item(0)->nodeValue : null;
        $nodelist = $xpath->query("xbrli:endDate", $period);
        $endDate = $nodelist->length === 1 ? $nodelist->item(0)->nodeValue : null;
        printf("%s<br>%s", $startDate, $endDate);
    }
    else
        ; // not found or more than one <xbrli:context id='D2010Q1'><xbrli:period>
    

    Old:

    $xmldoc = new DOMDocument();
    $xmldoc->load("http://www.sec.gov/Archives/edgar/data/27419/000110465911031717/tgt-20110430.xml");
    $xpath = new DOMXPath($xmldoc);
    $xpath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
    $nodelist = $xpath->query("/xbrli:xbrl/xbrli:context[@id='D2010Q1']/xbrli:period/xbrli:startDate"); // much faster than //xbrli:context and //xbrli:startDate
    if($nodelist->length === 1)
        print $nodelist->item(0)->nodeValue;
    else
        ; // not found or more than one <xbrli:context id='D2010Q1'><xbrli:period>