1

I am aware there are many similar posts, however I could not implement any of the suggestions successfully.

I have the following extract of Data - XML with two data sets available 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 file extract:

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

I am interested in retrieving the OBS_VALUE and TIME_PERIOD attributes from the 2nd data set. I would also like to count the observations within series.

Using the following suggestion my sample code looks like this:

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

Considering I do not retrieve the observation value and the count returns 0, I believe I am missing something very basic here. I also find it weird that

print_r($xml); 

returns:

SimpleXMLElement Object ( [frb] => SimpleXMLElement Object ( ) [comment] => SimpleXMLElement Object ( ) )

1 Answer 1

2

There are quite a few problems with the method your trying, this method uses XPath to find the data your after and this removes a lot of the need to move around the DOM to find data. The main thing with XPath though is to register the namespaces to allow you to use them in the query...

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

The XPath returns just the Obs elements (Note that XPath arrays are 1 based whereas PHP arrays are 0 based - which is why I use 2 to fetch the second element).

Sign up to request clarification or add additional context in comments.

1 Comment

OK - can you accept this answer if it solves your problem. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.