0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Erhnam
  • 901
  • 2
  • 13
  • 23
  • 1
    Can you state what output is coming and what you expected? – Alive to die - Anant Apr 13 '15 at 10:33
  • You don't display identifiers in your example (which is otherwise pretty much totally correct). So what is your question? The foreach you write (and ask about) sadly is not part of your question so it's not clear what you exactly ask about and where you probably made a little mistake. – hakre Apr 14 '15 at 05:54

2 Answers2

1

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'); 

}
har07
  • 88,338
  • 12
  • 84
  • 137
1

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);
}
Kevin
  • 41,694
  • 12
  • 53
  • 70