dongou3158 2015-04-13 10:22
浏览 103

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

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.

  • 写回答

2条回答 默认 最新

  • douli4337 2015-04-13 10:30
    关注

    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'); 
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示