在 PHP 中解析 XML 名称空间和架构

Parse XML namespace and schema in PHP

我知道有很多类似的帖子,但是我无法成功实施任何建议。

我有以下数据摘录 - XML 有两个数据集可用 here:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
  <message:MessageGroup xmlns:message="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message" xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/common" xmlns:frb="http://www.federalreserve.gov/structure/compact/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message SDMXMessage.xsd http://www.federalreserve.gov/structure/compact/common frb_common.xsd">

      <?frb EmbargoDate="2017-10-24T00:00:00"?>    

  <frb:DataSet id="Yields" xmlns:kf="http://www.federalreserve.gov/structure/compact/Yields_Yields" xsi:schemaLocation="http://www.federalreserve.gov/structure/compact/Yields_Yields Yields_Yields.xsd">
  </frb:DataSet>

  <frb:DataSet id="Yields" xmlns:kf="http://www.federalreserve.gov/structure/compact/Yields_Parameters" xsi:schemaLocation="http://www.federalreserve.gov/structure/compact/Yields_Parameters Yields_Parameters.xsd">
    <kf:Series BT="Nominal" CURRENCY="NA" FREQ="9" Parameter="B0" SERIES_NAME="BETA0" UNIT="Number" UNIT_MULT="1">
      <frb:Annotations>
        <common:Annotation>
          <common:AnnotationType>Short Description</common:AnnotationType>
          <common:AnnotationText>Beta0 Coefficient for Nominal Treasury Yields as Estimated by the Svensson Term Structure Model</common:AnnotationText>
        </common:Annotation>
      </frb:Annotations>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="3.91760612" TIME_PERIOD="1961-06-14"/>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="3.97849787" TIME_PERIOD="1961-06-15"/>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="3.98435045" TIME_PERIOD="1961-06-16"/>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="4.00437935" TIME_PERIOD="1961-06-19"/>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="3.98578922" TIME_PERIOD="1961-06-20"/>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="4.00405894" TIME_PERIOD="1961-06-21"/>
      <frb:Obs OBS_STATUS="A" OBS_VALUE="4.00089634" TIME_PERIOD="1961-06-22"/>
    </kf:Series>
  </frb:DataSet>
</message:MessageGroup>

YieldsParameters.xsd 文件提取:

xmlns:frb="http://www.federalreserve.gov/structure/compact/common">
<xs:import namespace="http://www.federalreserve.gov/structure/compact/common" schemaLocation="frb_common.xsd"/>

我有兴趣从第二个数据集中检索 OBS_VALUE 和 TIME_PERIOD 属性。我也想计算系列内的观察结果。

使用 following suggestion 我的示例代码如下所示:

$myFileXml = 'feds200628.xml';
$xml = simplexml_load_file($myFileXml);

$xml->DataSet[1]->Series[0] as $Ser
$ns_dc = $Ser ->children('http://www.federalreserve.gov/structure/compact/common');
echo $ns_dc->Obs[0]->->Attributes()->OBS_VALUE;

echo $count = count($xml->children('http://www.federalreserve.gov/structure/compact/common',true)->DataSet[1]->Series[0]->Obs);

考虑到我没有检索观察值和计数 returns 0,我相信我在这里遗漏了一些非常基本的东西。我也觉得

很奇怪
print_r($xml); 

returns:

简单XML元素对象 ( [frb] => 简单XML元素对象 ( ) [评论] => 简单XML元素对象 ( ) )

您尝试的方法存在很多问题,此方法使用 XPath 查找您之后的数据,这样就无需在 DOM 中移动来查找数据。 XPath 的主要作用是注册名称空间,以允许您在查询中使用它们...

$myFileXml = 'feds200628.xml';
$xml = simplexml_load_file($myFileXml);
$xml->registerXPathNamespace('frb', 'http://www.federalreserve.gov/structure/compact/common');
$xml->registerXPathNamespace('kf', "http://www.federalreserve.gov/structure/compact/Yields_Parameters");
$ns_dc = $xml->xpath("//frb:DataSet[2]/kf:Series[1]/frb:Obs");
echo $ns_dc[0]->Attributes()->OBS_VALUE.PHP_EOL;
$count = count($ns_dc);
echo $count.PHP_EOL;

XPath returns 只是 Obs 元素(请注意,XPath 数组是基于 1 而 PHP 数组是基于 0 - 这就是为什么我使用 2 来获取第二个元素)。