2012-05-23 96 views
1

盡我所能,我似乎無法抓住嵌套的apcm:Property元素中的「Id」屬性的值,其中「Name」屬性等於「sequenceNumber」,在線12.正如你所看到的那樣,感興趣的元素被埋在其他具有相同名稱和名稱空間的元素之中。與名稱空間和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

你需要註冊的命名空間,並使用[]謂詞,以確定你是哪個屬性元素感興趣。如果你不使用雙斜線,也就是說,如果你開始從該查找這是最安全的文檔元素。

<?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"; 
} 

?> 

注意,可能有理論上是多個兄弟特性元素用相同的@Name等等此XPath可以產生多個節點(@Id值)。

相關問題