PHP从XML字符串中的项获取id

时间:2015-04-13 10:22:35

标签: php xml xpath xbrl

我想要以下XML字符串中每个上下文项的id。

<?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>

我能够使用以下代码行解析XML;

$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");

但是,当我访问节点列表并打印每个节点时,不会显示任何标识符。有没有办法从XML中检索每个上下文块的id?例如context,conext_2等。

2 个答案:

答案 0 :(得分:1)

您可以使用/@attribute_name在xpath中选择节点的属性:

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

在循环getAttribute("attribute_name")节点列表时使用<context>

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

}

答案 1 :(得分:1)

您可以使用->getAttribute(),因为您已经正确获取了节点,只需迭代并将其指向节点并使用该方法:

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

Sample Output

->evaluate也可以使用:

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