2017-12-08 15 views
0

많은 유사한 게시물이 있음을 알고 있지만 제안 사항을 성공적으로 구현할 수 없습니다.PHP에서 XML 네임 스페이스 및 스키마 구문 분석

<?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 속성을 검색에 관심이 두 가지 데이터 세트 here와 XML -

나는 다음과 같은 데이터의 추출이 두 번째 데이터 집합에서. 나는 또한 일련의 관찰을 세고 싶다. 내가 관찰 값을 검색하지 않습니다 고려

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

카운트 내가 여기 아주 기본적인 뭔가를 놓친 거지 믿고, 0 반환 값 : following suggestion 내 샘플 코드를 사용

은 다음과 같습니다. 나는 또한 이상한 그

print_r($xml); 

반환 찾기 :

SimpleXMLElement 개체 ([FRB] => SimpleXMLElement 개체() [코멘트] => SimpleXMLElement 오브젝트())

답변

1

이 있습니다 시도하는 메소드에 대한 몇 가지 문제가 있습니다.이 메소드는 XPath를 사용하여 데이터를 찾으며 이로 인해 DOM을 돌아 다니면서 데이터를 찾을 필요가 없습니다. XPath를 가진 중요한 것은 비록

는 XPath를 그냥 것을 제외하고 요소 (PHP의 배열은 0 인 반면, 그 XPath를 어레이로 1입니다 참고 반환
$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; 

... 쿼리에서 사용할 수 있도록 네임 스페이스를 등록하는 것입니다 기반 - 두 번째 요소를 가져 오는 데 2를 사용하는 이유입니다).

+0

완벽 함, 고맙습니다. – bgolfb

+0

OK - 문제를 해결하면이 대답을 수락 할 수 있습니까? 감사 –