2012-05-23 4 views
1

시도해 보겠습니다. 중첩 된 apcm : 속성 요소의 "Id"속성 값을 가져올 수 없습니다. "Name"속성이 "sequenceNumber"와 같은 줄에 있습니다 보시다시피 관심있는 요소는 이름과 네임 스페이스가 동일한 다른 요소의 중첩 부분에 묻혀 있습니다.네임 스페이스와 PHP가있는 동일한 중첩 XML 요소

PHP를 사용하여 Id 값을 얻는 방법에 대해 머리를 감싸는 데 어려움을 겪고 있습니다.

<?xml version="1.0" encoding="utf-8" ?> 
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:apcm="http://ap.org/schemas/03/2005/apcm" xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:georss="http://www.georss.org/georss"> 
    <id>urn:publicid:ap.org:30085</id> 
<title type="xhtml"> 
    <apxh:div xmlns:apxh="http://www.w3.org/1999/xhtml"> 
     <apxh:span>AP New York State News - No Weather</apxh:span> 
    </apxh:div> 
</title> 
<apcm:Property Name="FeedProperties"> 
    <apcm:Property Name="Entitlement" Id="urn:publicid:ap.org:product:30085" Value="AP New York State News - No Weather" /> 
    <apcm:Property Name="FeedSequencing"> 
      <apcm:Property Name="sequenceNumber" Id="169310964" /> 
      <apcm:Property Name="minDateTime" Value="2012-05-22T18:04:18.913Z" /> 
    </apcm:Property> 
</apcm:Property> 
<updated>2012-05-22T18:04:18.913Z</updated> 
<author> 
    <name>The Associated Press</name> 
    <uri>http://www.ap.org</uri> 
</author> 
<rights>Copyright 2012 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.</rights> 
<link rel="self" href="http://syndication.ap.org/AP.Distro.Feed/GetFeed.aspx?idList=30085&amp;idListType=products&amp;maxItems=20" /> 
<entry> 
... 
</entry> 
</feed> 

답변

0

당신은 네임 스페이스를 등록하고 관심있는 속성 요소를 식별 할 [] 술어를 사용해야합니다. 당신은 당신이 시작하는 경우, 즉 이중 슬래시를 사용하여로부터 보이지 않는 경우가 가장 안전하다 문서 요소. 이론적으로 동일한 @Name 등이 XPath는 여러 노드들 (@Id 값)를 생성 할 수있다 여러 속성 형제 요소가있을 수 있음은

<?php 

$xml = <<<EOD 
... 
EOD; 

$sxe = new SimpleXMLElement($xml); 

$sxe->registerXPathNamespace('apcm', 'http://ap.org/schemas/03/2005/apcm'); 
$sxe->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); 

$result = $sxe->xpath('/atom:feed/acpm:Property[@Name=\'FeedProperties\']/acpm:Property[@Name=\'FeedSequencing\']/acpm:Property[@Name=\'sequenceNumber\']/@Id'); 

foreach ($result as $sequenceNumber) { 
    echo $sequenceNumber . "\n"; 
} 

?> 

참고.

관련 문제