Erhnam
Erhnam

Reputation: 921

PHP Get id's from items in XML string

I want the id of every context item in the following XML string.

<?xml version='1.0' encoding='UTF-8'?>
<xbrli:xbrl xmlns:xbrli="http://www.xbrl.org/2003/instance" id="xbrlID" xmlns:eba_met="http://www.eba.europa.eu/xbrl/crr/dict/met" xmlns:iso4217="http://www.xbrl.org/2003/iso4217" xmlns:xbrldi="http://xbrl.org/2006/xbrldi" xmlns:eba_dim="http://www.eba.europa.eu/xbrl/crr/dict/dim" xmlns:eba_MC="http://www.eba.europa.eu/xbrl/crr/dict/dom/MC" xmlns:eba_PL="http://www.eba.europa.eu/xbrl/crr/dict/dom/PL" xmlns:eba_BA="http://www.eba.europa.eu/xbrl/crr/dict/dom/BA" xmlns:eba_BT="http://www.eba.europa.eu/xbrl/crr/dict/dom/BT" xmlns:eba_CT="http://www.eba.europa.eu/xbrl/crr/dict/dom/CT" xmlns:eba_SC="http://www.eba.europa.eu/xbrl/crr/dict/dom/SC" xmlns:find="http://www.eurofiling.info/xbrl/ext/filing-indicators" xmlns:eba_AS="http://www.eba.europa.eu/xbrl/crr/dict/dom/AS" xmlns:link="http://www.xbrl.org/2003/linkbase" xmlns:xlink="http://www.w3.org/1999/xlink">
  <link:schemaRef xlink:type="simple" xlink:href="http://www.eba.europa.eu/eu/fr/xbrl/crr/fws/finrep/its-2013-03/2014-07-31/mod/finrep_con_ifrs.xsd"/>
  <xbrli:context id="context">
    <xbrli:entity>
      <xbrli:identifier scheme="http://scheme">31489</xbrli:identifier>
    </xbrli:entity>
    <xbrli:period>
      <xbrli:instant>2014-12-31</xbrli:instant>
    </xbrli:period>
    <xbrli:scenario>
      <xbrldi:explicitMember dimension="eba_dim:APL">eba_PL:x26</xbrldi:explicitMember>
      <xbrldi:explicitMember dimension="eba_dim:BAS">eba_BA:x7</xbrldi:explicitMember>
      <xbrldi:explicitMember dimension="eba_dim:MCY">eba_MC:x111</xbrldi:explicitMember>
    </xbrli:scenario>
  </xbrli:context>
  <xbrli:context id="context_2">
    <xbrli:entity>
      <xbrli:identifier scheme="http://scheme">31489</xbrli:identifier>
    </xbrli:entity>
    <xbrli:period>
      <xbrli:instant>2014-12-31</xbrli:instant>
    </xbrli:period>
    <xbrli:scenario>
      <xbrldi:explicitMember dimension="eba_dim:APL">eba_PL:x26</xbrldi:explicitMember>
      <xbrldi:explicitMember dimension="eba_dim:BAS">eba_BA:x7</xbrldi:explicitMember>
      <xbrldi:explicitMember dimension="eba_dim:MCY">eba_MC:x99</xbrldi:explicitMember>
    </xbrli:scenario>
  </xbrli:context>
</xbrli:xbrl>

I'm able to parse the XML with the following lines of code;

$xmldoc = new DOMDocument();
$xmldoc->load($xml);
$xpath = new DOMXPath($xmldoc);
$xpath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
$nodelist = $xpath->query("/xbrli:xbrl/xbrli:context");

However when I foreach the nodelist and print every node, no identifier is displayed. Is there any way to retrieve the id of every context block from the XML? e.g. context, conext_2, etc.

Upvotes: 0

Views: 259

Answers (2)

Kevin
Kevin

Reputation: 41885

You could just use ->getAttribute() since you already got the nodes correctly, just iterate and point it to the node and use the method:

foreach($nodelist as $node) {
    echo $node->getAttribute('id');
}

Sample Output

->evaluate will work as well:

foreach($nodelist as $node) {
    echo $xpath->evaluate('string(./@id)', $node);
}

Upvotes: 1

har07
har07

Reputation: 89285

You can use /@attribute_name to select node's attribute in xpath :

$idlist = $xpath->query("/xbrli:xbrl/xbrli:context/@id");

Or using getAttribute("attribute_name") while looping list of <context> node :

foreach ($nodelist as $node){
    //do something useful with $id    
    $id = $node->getAttribute('id'); 

}

Upvotes: 1

Related Questions