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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同